Last Updated : 10 Apr, 2024
#include <iostream>
#include <vector>
using namespace std;

int main() 
{
    vector <int> vec = {1, 2, 3, 4, 5, 6};
    vector <int> :: iterator itr;  // line 1 
    
       for(itr = vec.end(); itr >= vec.begin(); itr--)
     cout << *itr << \" \";
    
    cout << endl;
    
    for(itr = vec.end()-1; itr >= vec.begin(); itr--)
     cout << *itr << \" \";
   return 0;
} 

Output for the above program
(A) 6 5 4 3 2 1
6 5 4 3 2 1
(B) garbage 6 5 4 3 2 1
6 5 4 3 2 1
(C) 6 5 4 3 2 1
0 5 4 3 2 1
(D) error in line 1


Answer: (B)

Explanation:

vector  :: iterator

above is the declaration of iterator of type, thus line 1 works fine. The first loop traverses vector and prints the element with the garbage value. The second traversal gives no garbage value and prints the element of the vector in reverse direction.



Share your thoughts in the comments