← Quiz: Iterators in C++: all 10 questions
Quiz: Iterators in C++ · Question 6 of 10
What does this print? (code)
What does this print?
#include <iostream>
#include <iterator>
#include <list>
using namespace std;
int main() {
list<int> l = {1, 2, 3, 4, 5, 6};
auto it = l.begin();
advance(it, 4);
cout << *it << " " << distance(l.begin(), it) << " " << *prev(it) << "\n";
return 0;
}
- A.4 4 3
- B.5 5 4
- C.5 4 4
- D.4 3 5
Answer & explanation
Correct answer: 5 4 4
Moving 4 steps from the first element reaches 5 at position 4, and the element before it is 4.
Can you get the rest right without peeking?
Take the timed Quiz: Iterators in C++ quiz free. Pass it to earn XP and count towards your certificate.
Start the quiz →