tags:

views:

72

answers:

4

Is iterator invalidated after:

string b "Some string";
auto beg_ = b.begin();
auto end_ = b.end();
b.erase(beg_);
+5  A: 

Yes, but erase return a valid iterator you can use to continue in a loop :

For the remaining members, the function returns an iterator of member type string::iterator referring to the character that now occupies the position of the first character erased, or, if no such character exists, returns end().

Source : http://www.cplusplus.com/reference/string/string/erase/

Klaim
+2  A: 

Yes, Standard says

References, pointers, and iterators referring to the elements of a basic_string sequence may be invalidated by the following uses of that basic_string object:

  • Calling non-const member functions, except operator, at(), begin(), rbegin(), end(), and rend().
Johannes Schaub - litb
+1  A: 

Yes

http://www.sgi.com/tech/stl/basic_string.html

Iterators may be invalidated by swap, reserve, insert, and erase (and by functions that are equivalent to insert and/or erase, such as clear, resize, append, and replace). Additionally, however, the first call to any non-const member function, including the non-const version of begin() or operator[], may invalidate iterators.

Tristram Gräbener
+1  A: 

From what I understand, yes. Erase and insert operations invalidate iterators.

But erase(iterator) also returns an iterator you may be able to use.

suicideducky