1

Is iterator invalidated after:

string b "Some string"; auto beg_ = b.begin(); auto end_ = b.end(); b.erase(beg_); 

4 Answers 4

5

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/

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

1

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

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

Comments

1

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().

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.