Home >Backend Development >C++ >How Can I Safely Remove Elements from a C Map While Preserving Iterator Validity?
Iterative Removal from Maps: Preserving Iterators
In map iteration, removing elements can be a tricky operation as it can invalidate iterators. To prevent this, programmers often seek solutions that preserve iterator validity.
The standard solution for this problem is the associative-container erase idiom:
for (auto it = m.cbegin(); it != m.cend() /* not hoisted */; /* no increment */) { if (must_delete) { m.erase(it++); // or "it = m.erase(it)" since C++11 } else { ++it; } }
Crucially, a regular for loop must be used here rather than a range-based-for loop (RBFL) because the container is being modified. In an ordinary for loop, the iterator is incremented explicitly, providing control over the iteration process. RBFLs, which automatically iterate over elements, conceal the iteration mechanism and could lead to undefined behavior.
For pre-C 11 implementations, the syntax is slightly different:
for (std::map<K,V>::iterator it = m.begin(); it != m.end(); ) { /* ... */ }
Here, const iterators cannot be erased. Consequently, a dedicated iterator that accommodates erasing must be used.
The above is the detailed content of How Can I Safely Remove Elements from a C Map While Preserving Iterator Validity?. For more information, please follow other related articles on the PHP Chinese website!