Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is return value from std::vector erase operator, according to the standard?

I prefer to get info from the source, for this case this is ISO-IEC 14882, where erase method is described as the following:

"iterator erase(const_iterator position);
iterator erase(const_iterator first, const_iterator last);

Effects: Invalidates iterators and references at or after the point of the erase.

Complexity: The destructor of T is called the number of times equal to the number of the elements erased, but the move assignment operator of T is called the number of times equal to the number of elements in the vector after the erased elements.

Throws: Nothing unless an exception is thrown by the copy constructor, move constructor, assignment operator, or move assignment operator of T."

Can't find information about returned iterator, of course, I Googled and got:

An iterator pointing to the new location of the element that followed the last element erased by the function call

Can't understand there this is described in the standard
Could you point me on it?

Update: my question is not about how vector::erase works,
but rather from where in the standard, as I accept as reliable source of information we can deduce information about returned value

like image 257
spin_eight Avatar asked Dec 08 '22 13:12

spin_eight


1 Answers

The information is in a slightly un-intuitive place. What erase returns is detailed under the sequence containers section of general container requirements section, specificly [sequence.reqmts]/11

The iterator returned from a.erase(q) points to the element immediately following q prior to the element being erased. If no such element exists, a.end() is returned.

and [sequence.reqmts]/12

The iterator returned by a.erase(q1, q2) points to the element pointed to by q2 prior to any elements being erased. If no such element exists, a.end() is returned.

like image 58
NathanOliver Avatar answered Jun 01 '23 13:06

NathanOliver