C++ STL Containers,Iterators and Algorithms #include <iostream> using std::cout;
ID: 3828785 • Letter: C
Question
C++ STL Containers,Iterators and Algorithms
#include <iostream> using std::cout; using std::endl; int main(){ cout << "---------------------- STL vector ---------------------- "; // declare container here cout << "Enter numbers to store in STL containers (enter a letter to stop) "; // read in integers from STDIN here and store them in the vector cout << "There are " << << " integers in the vector" << endl; // print out the contents of the container in the reverse order that they were read in cout << "---------------------- STL list ---------------------- "; // declare container here // copy elements from the vector into this container (in the order that they were entered) cout << "There are " << << " integers in the list" << endl; // print out the contents of the container in the reverse order that they were read in // Hint: Make a copy of the elements into a second list first . . . cout << "---------------------- STL deque ---------------------- "; // declare container here // copy elements from the vector into this container (in the order that they were entered) cout << "There are " << << " integers in the deque" << endl; // print out the contents of the container in the reverse order that they were read in cout << "---------------------- STL stack ---------------------- "; // declare container here // Optionally, for a challenge, specify the underlying container (and it's type) when declaring the stack container (and it's type) // copy elements from the vector into this container (in the order that they were entered) cout << "There are " << << " integers in the stack" << endl; // print out the contents of the container in the reverse order that they were read in cout << "---------------------- STL queue ---------------------- "; // declare container here // copy elements from the vector into this container (in the order that they were entered) cout << "There are " << << " integers in the queue" << endl; // print out the contents of the container in the reverse order that they were read in // Hint: For each element, make a temporary queue with all of the remaining elements in it . . . cout << "---------------------- STL priority_queue ---------------------- "; // declare container here // copy elements from the vector into this container (in the order that they were entered) cout << "There are " << << " integers in the priority_queue" << endl; cout << "Priority queues do not keep track of the insertion order of their contents, but here's it's contents: "; // print out the contents of the container cout << " Can the number of elements and the contents be printed out in the order that they were entered for associative containers? Why? "; /* * Answer the question here with a cout statement */ /* * lab17B */ cout << " ---------------------- Printing out the contents in the ordered entered using iterators ---------------------- "; cout << "---------------------- STL vector ---------------------- "; // use STL vector iterators to print out the values stored in the container if possible (otherwise print out why it's not possible) cout << "---------------------- STL array ---------------------- "; // use STL array iterators to print out the values stored in the container if possible (otherwise print out why it's not possible) cout << "---------------------- STL list ---------------------- "; // use STL list iterators to print out the values stored in the container if possible (otherwise print out why it's not possible) cout << "---------------------- STL forward_list ---------------------- "; // use STL forward_list iterators to print out the values stored in the container if possible (otherwise print out why it's not possible) cout << "---------------------- STL deque ---------------------- "; // use STL deque iterators to print out the values stored in the container if possible (otherwise print out why it's not possible) cout << "---------------------- STL stack ---------------------- "; // use STL stack iterators to print out the values stored in the container if possible (otherwise print out why it's not possible) cout << "---------------------- STL queue ---------------------- "; // use STL queue iterators to print out the values stored in the container if possible (otherwise print out why it's not possible) cout << "---------------------- STL priority_queue ---------------------- "; // use STL priority_queue iterators to print out the values stored in the container if possible (otherwise print out why it's not possible) cout << " ---------------------- Determine the unique elements in the vector (using STL algorithms) and print them out ---------------------- "; // determine the unique elements in the vector (using STL algorithms) // Hint: You'll need to use a couple of different functions cout << "There are a total of " << << " unique integers: "; // print out the unique elements return 0; }
Explanation / Answer
/******************************************************************************
Welcome to GDB Online.
GDB online is an online compiler and debugger tool for C/C++.
Code, Compile, Run and Debug online from anywhere in world.
*******************************************************************************/
#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
#include <list>
#include <queue>
#include <deque>
#include <stack>
using namespace std;
using std::cout;
using std::endl;
int main(){
cout << "---------------------- STL vector ---------------------- ";
// declare container here
vector<int> vec;
int temp;
cout << "Enter numbers to store in STL containers (enter a letter to stop) ";
cin>>temp;
while(cin)
{
vec.push_back(temp);
cin>>temp;
}
cout << "There are " << vec.size() << " integers in the vector " << endl;
// print all element in reverse order
cout<<"Elements of Vector in reverse order ";
copy (vec.rbegin(), vec.rend(), // source
ostream_iterator<int>(cout," ")); // destination
cout << endl;
cout << "---------------------- STL list ---------------------- ";
// declare container here
std::list<int> list_sample;
// copy elements from the vector into this container (in the order that they were entered)
std::copy( vec.begin(), vec.end(), std::back_inserter( list_sample ) );
cout << "There are " << list_sample.size() << " integers in the list" << endl;
std::list<int> copy_list (list_sample);
copy_list.reverse();
cout<<"Elements of List in reverse order ";
for (std::list<int>::iterator it=copy_list.begin(); it!=copy_list.end(); ++it)
std::cout << ' ' << *it;
cout << " ---------------------- STL deque ---------------------- ";
// declare container here
std::deque<int> first_deque;
std::copy( vec.begin(), vec.end(), std::back_inserter( first_deque ) );
cout << "There are " << first_deque.size() << " integers in the deque " << endl;
cout<<"Elements of deque in reverse order ";
std::deque<int> dq2(first_deque.rbegin(), first_deque.rend());
for (long i=0; i<(long)dq2.size(); ++i) cout << dq2.at(i) << " ";
cout << " ---------------------- STL stack ---------------------- ";
// declare container here
std::stack<int, std::vector<int>> first_stack(vec);
cout << "There are " << first_stack.size() << " integers in the stack " << endl;
std::stack<int> s2;
while (!first_stack.empty())
{
cout<<first_stack.top()<<" ";
first_stack.pop();
}
cout << " ---------------------- STL queue ---------------------- ";
// declare container here
std::queue<int> first_queue;
for(int i = 0; i < vec.size(); i++)
{
first_queue.push(vec[i]);
}
cout << "There are " << first_queue.size() << " integers in the queue " << endl;
while(!first_queue.empty())
{
cout<<first_queue.front()<<" ";
first_queue.pop();
}
cout << " ---------------------- STL priority queue ---------------------- ";
std::priority_queue<int> first_pqueue;
for(int i = 0; i < vec.size(); i++)
{
first_pqueue.push(vec[i]);
}
cout << "There are " << first_pqueue.size() << " integers in the priority queue " << endl;
cout << " Priority queues do not keep track of the insertion order of their contents, but here's it's contents: ";
std::priority_queue<int> temporary = first_pqueue;
while (!temporary.empty()) {
std::cout << temporary.top() << " ";
temporary.pop();
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.