C++ set iterator removal
c++, set, stl
Solution
Is it allowed to erase an element pointed by iterator, and advance the same iterator in one line to go to next element?
Yes, it is valid.
Rationale:
`it++` increments `it` so that it refers to the next element but yields a copy of its original value. Thus, `it` doesn't refer to the element that is removed when `erase()` is called. And in case of `std::set` only iterators to the erased element are invalidated.#1
You can consider this code example as a boilerplate code to remove an element to which your iterator is referring.
References:
For `std::set`, #1C++03 Standard 23.1.2/8:
Only iterators and references to the erased elements are invalidated
Problem
Is it allowed to erase an element pointed by iterator, and advance the same iterator in one line to go to next element? ``` set<int>::iterator it = S.begin(); while (it != S.end()) { if (shouldBeRemoved(*it)) { S.erase(it++); // is this line valid? } else { ++it; } } ```