What will happen when you attempt to compile and run the following code?

Last Updated on June 24, 2022 by Admin

What will happen when you attempt to compile and run the following code?

#include <iostream>
#include <algorithm>
#include <vector>
#include <deque>
#include <set>
using namespace std;
struct myprinter
{
   void operator () (int i)
     {
        cout << i <<", ";
     }
};
int 
main() {
   int mynumbers[] = { 3, 9, 0, 2, 1, 4, 5 };
   vector < int > v1 (mynumbers, mynumbers + 7);
   deque < int > d1 (mynumbers, mynumbers + 7);
   set < int > s1 (mynumbers, mynumbers + 7);
   v1.pop_back (5);   //LINE I
   for_each (s1.begin (), s1.end (), myprinter ());   //LINE II
   for_each (d1.begin (), d1.end(), *(new myprinter()));  //LINE III
   for_each (v1.begin (), v1.end(), myprinter);  //LINE IV
   return 0;
}
  • the program outputs 3, 9, 0, 2, 1, 4, 5, 0, 1, 2, 3, 4, 5, 9,
  • the program outputs 6, 18, 0, 4, 2, 8, 10, 6, 18, 0, 4, 2, 8, 10,
  • the program outputs 6, 18, 0, 4, 2, 8, 10, 0, 2, 4, 6, 8, 10, 18,
  • compilation error in LINE III
  • compilation error in LINE I
  • compilation error in LINE II 
  • the compilation will be thrown at LINE I
5 1 vote
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments