← 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?
- A.It keeps the removed elements
- B.It sorts the vector
- C.It removes all matching elements in a single O(n) pass instead of shifting the vector for every erase
- 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 →