LearnFrenzy
Sign in Take a free test

Arrays and Vectors · Arrays and Vectors MCQ · Question 13

What can happen to iterators after push_back? Answer, explanation & discussion

  • MCQ
  • Hard
  • 0 comments
  • Answer explained
  • Moderated community
Question 13 Back to Exercise

A program stores auto it = v.begin(); and then calls v.push_back(x);. Is it still valid?

  • A Always
  • B Not if the push_back reallocated – then all iterators, pointers and references to elements are invalid Correct
  • C Only if v was empty
  • D Iterators are never invalidated
Correct Answer: Option B

Reallocation moves the elements to a new buffer. Re-obtain iterators after growing, or reserve enough capacity first.

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links