← Quiz: Iterators in C++: all 10 questions
Quiz: Iterators in C++ · Question 7 of 10
Why can pushback on a vector make an old iterator into it unsafe?
Why can push_back on a vector make an old iterator into it unsafe?
- A.push_back always sorts the vector
- B.If capacity runs out, the vector moves all elements to new memory, so old iterators point to freed memory
- C.push_back deletes the first element
- D.Iterators expire after one second
Answer & explanation
Correct answer: If capacity runs out, the vector moves all elements to new memory, so old iterators point to freed memory
Reallocation invalidates every iterator, pointer and reference into the vector.
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 →