I have the following code:
set<Key> test; test.insert(key1); test.insert(key2); iter1 = test.find(key1); iter2 = test.find(key2); test.erase(iter1);
My question is: if key1 is removed, now can I use iter2 to access key2 in the test?
thank
Yes, set eraseinvalidates only iterators pointing to the element to be erased (note that this is not necessarily true for all containers).
erase
, "" , key1 key2 ; iter1 == iter2 iter1 iter2. . , .
key1
key2
iter1 == iter2
iter1
iter2
:
struct Foo { Foo(std::string s = "") : s(s) { } bool operator<(const Foo & other) { return s.size() < other.size(); } } std::set<Foo> x; x.insert(Foo("Cat")); x.insert(Foo("Dog")); // booboo
, , .
deque , ( ) deque (23.2.1.3/4), (23.2.2.3/3), (23.2.4.3/3)