🎙 Free live masterclass · Sun, 29 Nov, 7:00 pm IST · Java Full Stack roadmap Register free →
Skills101
← 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?

  1. A.push_back always sorts the vector
  2. B.If capacity runs out, the vector moves all elements to new memory, so old iterators point to freed memory
  3. C.push_back deletes the first element
  4. 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 →