What will be the output of the following C++ code? [code] #include<iostream> #include<iterator> #include<vector> using namespace std;

What will be the output of the following C++ code?
Code:
#include<iostream> 
#include<iterator> 
#include<vector> 
using namespace std; 
int main() 
{ 
    vector<int> ar = { 1, 2, 3, 4, 5 }; 
    vector<int>::iterator ptr = ar.begin(); 
    advance(ptr, 2);
    cout << *ptr << endl; 
    return 0; 
}
A. 2
B. 3
C. 4
D. 5
 
Answer: b
Explanation: Initially the ptr is pointing to first element of vector and now as we are advancing the iterator by 2 which takes the iterator the value 3. Hence the output is 3.
Output:
$ ./a.out
3
 

Tìm kiếm

Back
Top