An iterator it points into a std::map. The statement it = m.erase(it); is executed. What does it hold afterwards?
Correct answer
B. The iterator to the following element, which erase returned by value
15 views
B. The iterator to the following element, which erase returned by value
15 views
std::map::erase returns an iterator to the element after the erased one, which is the standard erase-while-iterating idiom in C++11 and later. Only iterators to the erased element are invalidated; all others, and every reference, remain valid. The C++03 void-returning erase instead needs the it = m.erase(it++) workaround.
Asked in: NTS, PPSC IT, Campus Interviews
Not sure why the answer is B? Ask here and get an explanation.