Erase without invalidating iterator
This article describes the pattern I prefer to write such a loop in the correct way.If you search for this problem you'll find lots of other styles to write such a loop (for example on stackoverflow), but I don't like all answers I've found for three reasons: loop body in which is is mainly used.This is an experiment to test the difference between C iterator of STL vector and list affected by erase operation and list performs like real linked list.The official saying or description about C iterators of vector and list are listed as follows: for list erase: Data races The container is modified. Concurrently accessing or modifying other elements is safe, although iterating ranges that include the removed elements is not.However, as iterators are based on counting, they are all invalidated by addition. Only the iterators and references to the erased element is invalidated Reason: As per addition. Only iterators and references to the erased elements are invalidated.
------------- after erase for *vec*: map(3): key = 3, value = 4 The old iterator for element 4 is point to the last element of the newly vector!
Details: I've recently been brushing up on my C and learning my way around C 11.
As part of that, I've been writing an idiomatic wrapper around the uriparser library.
Deleting an arbitrary number of elements is generally written this way by most beginners: is called and so the itr statement in the for() loop fails.
Most modern versions of STL have debugging code to warn you if you use an invalidated iterator, but no such training wheels existed in the early days of STL.