Goofy
Goofy

Reputation: 5397

C++ containers' iterators

This question may sounds lame, but I haven't found clear answer. Can I assume, that iterator returned by map's find() method will this points at the same data even if I'll add (or remove) other elements to the same map? It's unclear for me if map's iterator points at position in map or at specific data...

Upvotes: 3

Views: 206

Answers (1)

interjay
interjay

Reputation: 110202

You can keep using the same iterator after insert or erase operations. The standard says (C++03 standard, Section 23.1.2, paragraph 8):

The insert members shall not affect the validity of iterators and references to the container, and the erase members shall invalidate only iterators and references to the erased elements.

This is true for all associative containers (map, set, multimap, multiset).

Upvotes: 11

Related Questions