Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ STL Iterators and ALgorithms half of code given , extend the rest #include <

ID: 3826291 • Letter: C

Question

C++ STL Iterators and ALgorithms

half of code given , extend the rest

#include <iostream>

#include <vector>

#include <list>

#include <deque>

#include <stack>

#include <queue>

using namespace std;

int main(){

   cout << "---------------------- STL vector ---------------------- ";

   // declare container here

   vector<int> vec;

   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

   while(true)

   {

       int n;

       if(cin>>n)

           vec.push_back(n);

       else

           break;

   }

   cout << "There are " <<vec.size()<< " integers in the vector" << endl;

   // print out the contents of the container in the reverse order that they were read in

   for(vector<int>::reverse_iterator it = vec.rbegin(); it != vec.rend(); ++it)

   {

       cout << *it <<endl;

   }

   cout << "---------------------- STL list ---------------------- ";

   // declare container here

   list <int> lt;

   // copy elements from the vector into this container (in the order that they were entered)

   lt.assign(vec.begin(),vec.end());

   cout << "There are " <<lt.size() << " 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 . . .

   for(list<int>::reverse_iterator it = lt.rbegin(); it != lt.rend(); ++it)

   {

       cout << *it <<endl;

   }

   cout << "---------------------- STL deque ---------------------- ";

   // declare container here

   deque<int> dq;

   // copy elements from the vector into this container (in the order that they were entered)

   dq.assign(vec.begin(),vec.end());

   cout << "There are " << dq.size() << " integers in the deque" << endl;

   // print out the contents of the container in the reverse order that they were read in

   for(deque<int>::reverse_iterator it = dq.rbegin(); it != dq.rend(); ++it)

   {

       cout << *it <<endl;

   }

   cout << "---------------------- STL stack ---------------------- ";

   // declare container here

   stack<int> st;

   // 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)

   for(vector<int>::iterator it=vec.begin();it!=vec.end();++it)

       st.push(*it);

   cout << "There are " <<st.size() << " integers in the stack" << endl;

   // print out the contents of the container in the reverse order that they were read in

   while(st.empty()== false)

   {

       cout<<st.top()<<endl;

       st.pop();

   }

   cout << "---------------------- STL queue ---------------------- ";

   // declare container here

   queue<int> que;

   // copy elements from the vector into this container (in the order that they were entered)

   for(vector<int>::iterator it=vec.begin();it!=vec.end();++it)

       que.push(*it);

   cout << "There are " <<que.size()<< " 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 . . .

   stack<int> temp;

   while(que.empty()==false)

   {

       temp.push(que.front());//copy to another container

       que.pop();

   }

   //print that temporary container data

   while(temp.empty()== false)

   {

       cout<<temp.top()<<endl;

       temp.pop();

   }

   cout << "---------------------- STL priority_queue ---------------------- ";

   // declare container here

   // copy elements from the vector into this container (in the order that they were entered)

   priority_queue<int> pq(vec.begin(),vec.end());

   cout << "There are " <<pq.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: ";

   // print out the contents of the container

   // print out the contents of the container in the reverse order that they were read in

   while(!pq.empty()) {

       cout << pq.top() <<endl;

       pq.pop();

   }

   cout << " Can the number of elements and the contents is printed out in the order that they were entered for associative containers? Why? ";

   /*

   * Answer the question here with a cout statement

   */

   cout<<" containers are used for different purpose. Depends on use of container and size of container are needed by programmer. ";

   return 0;

}


/ * now add to that code , fix below so it will compile and also do the task it ask for each cout */

Explanation / Answer

#include <iostream>
#include <vector>
#include <list>
#include <deque>
#include <stack>
#include <queue>
#include <set>

using namespace std;

int main() {

   cout << "---------------------- STL vector ---------------------- ";
   // declare container here
   vector<int> vec;

   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
   while (true) {
       int n;
       if (cin >> n)
           vec.push_back(n);
       else
           break;
   }

   cout << "There are " << vec.size() << " integers in the vector" << endl;

   for (vector<int>::iterator it = vec.begin(); it != vec.end();
           ++it) {
       cout << *it << endl;
   }

   cout << "---------------------- STL list ---------------------- ";
   // declare container here
   list<int> lt;

   // copy elements from the vector into this container (in the order that they were entered)
   lt.assign(vec.begin(), vec.end());

   cout << "There are " << lt.size() << " integers in the list" << endl;

   cout << "list always maintains the order in which element inserted." << endl;
   // Hint: Make a copy of the elements into a second list first . . .
   for (list<int>::iterator it = lt.begin(); it != lt.end(); ++it) {
       cout << *it << endl;
   }

   cout << "---------------------- STL deque ---------------------- ";
   // declare container here
   deque<int> dq;
   // copy elements from the vector into this container (in the order that they were entered)
   dq.assign(vec.begin(), vec.end());
   cout << "There are " << dq.size() << " integers in the deque" << endl;

   // print out the contents of the container in the reverse order that they were read in
   for (deque<int>::reverse_iterator it = dq.rbegin(); it != dq.rend(); ++it) {
       cout << *it << endl;
   }

   cout << "---------------------- STL stack ---------------------- ";
   // declare container here
   stack<int> st;
  
   // copy elements from the vector into this container (in the order that they were entered)
   for (vector<int>::iterator it = vec.begin(); it != vec.end(); ++it)
       st.push(*it);

   cout << "There are " << st.size() << " integers in the stack" << endl;

   cout << "Stack works on LIFO( last in first out manner ) so contents of the container will get printed in the reverse order." << endl;
   while (!st.empty()) {
       cout << st.top() << endl;
       st.pop();
   }

   cout << "---------------------- STL queue ---------------------- ";
   // declare container here
   queue<int> que;
   cout << "Queue works on FIFO ( first IN first Out) manner so elements will get copied or printed from the vector into this container is same as in the order that they were entered" << endl;
   for (vector<int>::iterator it = vec.begin(); it != vec.end(); ++it)
       que.push(*it);
   cout << "There are " << que.size() << " 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 . . .
   stack<int> temp;
   while (que.empty() == false) {
       cout << que.front() << endl;
       que.pop();
   }
  
   cout
           << "---------------------- STL priority_queue ---------------------- ";
   // declare container here   

   // copy elements from the vector into this container (in the order that they were entered)
   priority_queue<int> pq(vec.begin(), vec.end());

   cout << "There are " << pq.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: ";
   cout
           << "Priority takes element's priority too as an input while inserting the same , but here we don't have that much info so it will simply works as like queue. ";      
   // print out the contents of the container
   // print out the contents of the container in the reverse order that they were read in
   while (!pq.empty()) {
       cout << pq.top() << endl;
       pq.pop();
   }
  
   set<int> first;
  
   cout
           << "---------------------- STL Unique elements ---------------------- ";
for (vector<int>::iterator it = vec.begin(); it != vec.end();
           ++it) {
   first.insert(*it);
   }
  
   for (set<int>::iterator it = first.begin(); it != first.end();
           ++it) {
       cout << *it << endl;
   }

   return 0;
}

Output

-------------

There are 3 integers in the vector   
10   
20   
30   
---------------------- STL list ----------------------   
There are 3 integers in the list   
list always maintains the order in which element inserted.   
10   
20   
30   
---------------------- STL deque ----------------------
There are 3 integers in the deque
30   
20   
10   
---------------------- STL stack ----------------------
There are 3 integers in the stack
Stack works on LIFO( last in first out manner ) so contents of the container will get printed in the reverse order.
30   
20   
10   
---------------------- STL queue ----------------------
Queue works on FIFO ( first IN first Out) manner so elements will get copied or printed from the vector into this container is same as in the order that they were en
tered
There are 3 integers in the queue
10   
20   
30   
---------------------- STL priority_queue ----------------------   
There are 3 integers in the priority_queue   
Priority queues do not keep track of the insertion order of their contents, but here's it's contents:
Priority takes element's priority too as an input while inserting the same , but here we don't have that much info so it will simply works as like queue.
30   
20   
10
---------------------- STL Unique elements ----------------------   
12   
24   

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote