Will the erase function set in C ++ change the address of other elements?

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

+5
source share
3 answers

Yes, set eraseinvalidates only iterators pointing to the element to be erased (note that this is not necessarily true for all containers).

+7
source

, "" , 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
+3

, , .

deque , ( ) deque (23.2.1.3/4), (23.2.2.3/3), (23.2.4.3/3)

+3
source

All Articles