🎙 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 9 of 10

What does the erase–remove idiom v.erase(removeif(v.begin(), v.end(), pred), v.end()) gain over erasing one element at…

What does the erase–remove idiom v.erase(remove_if(v.begin(), v.end(), pred), v.end()) gain over erasing one element at a time?

  1. A.It keeps the removed elements
  2. B.It sorts the vector
  3. C.It removes all matching elements in a single O(n) pass instead of shifting the vector for every erase
  4. D.It works only on lists
Answer & explanation

Correct answer: It removes all matching elements in a single O(n) pass instead of shifting the vector for every erase

remove_if compacts the kept elements once; one erase then trims the tail.

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 →