What will be the output of the following C++ code? [code] #include <iostream> #include <vector> using namespace std; int main() {

What will be the output of the following C++ code?
Code:
#include <iostream> 
#include <vector> 
using namespace std; 
int main() 
{ 
    vector<int> v; 
    for (int i = 1; i <= 5; i++) 
        v.push_back(i); 
    vector<int> :: const_iterator i;
   	for (i = v.begin(); i != v.end(); ++i) 
        cout << *i << " ";
    cout<<endl;
    return 0; 
}
A. 1 2 3 4 5
B. 1 3 5
C. 1 4 5
D. Error
 
Answer: a
Explanation: A normal iterator can be stored in const_iterator therefore program does not gives any error hence will be executed perfectly.
Output:
$ ./a.out
1 2 3 4 5
 

Tìm kiếm

Back
Top