Question PRG-CPP-0020
HardWhy is iterating a std::vector and erasing elements inside a range-for loop dangerous?
- A.
vectors can't be erased
- B.
range-for loops can't modify elements
- C.
Sign up free to see the answer and a step-by-step explanation.
Popular companies
Prepare company by company
Hiring pattern, syllabus, past questions and mocks for each company.
All companiesPractice by subject
C++ question
Why is iterating a std::vector and erasing elements inside a range-for loop dangerous?
vectors can't be erased
range-for loops can't modify elements
Sign up free to see the answer and a step-by-step explanation.
erase invalidates iterators at and after the erased position, so the loop's iterator becomes invalid
It is always safe