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<vector>
#include<iostream>
#include<algorithm>

using namespace std;
void
printer(int v)
{
  cout<<v<<", ";
}
struct Sequence
{
  int start;
  Sequence(int start):start (start){}
  int operator() ()
  {
    return start++; //LINE I
  }
};

int
main()
{
  vector<int> v1(5);
  generate_n(v1.begin(), 5, Sequence(5)); //LINE II
  for_each(v1.begin(), v1.end(), printer);
  return 0;
}
  • compilation error in LINE I
  • the program outputs 5, 5, 5, 5, 5,
  • compilation error in LINE II
  • the program outputs 5, 6, 7, 8, 9,
  • the program outputs 1, 2, 3, 4, 5,
  • runtime error at LINE II
5 1 vote
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments