By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,589 Members | 1,202 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,589 IT Pros & Developers. It's quick & easy.

off_the_end iterator to vector::erase() function

P: n/a
Consider
vector<stringv;

If we call,

v.erase(v.end())

this invokes undefined behaviour.

But, if we call

v.erase(v.begin(), v.end())
or
v.erase(v.end(), v.end())

will these last two calls invoke undefined behaviour ? Why I am asking
is that v.clear() on an empty vector is valid - it does nothing.
v.clear() is equivalent to v.erase(v.begin(), v.end()) which in turn
is equal to v.erase(v.end(), v.end()) when 'v' is empty. Am I
correct ?

Kindly clarify.

Thanks
V.Subramanian
Jun 27 '08 #1
Share this Question
Share on Google+
3 Replies


P: n/a
My views. Guys, correct me if I am wrong.

~Moh

On Apr 30, 9:23*pm, "subramanian10...@yahoo.com, India"
<subramanian10...@yahoo.comwrote:
Consider
vector<stringv;

If we call,

v.erase(v.end())

this invokes undefined behaviour.
Correct. Reason: According to 23.1.1 point 3 and 4 operation erase is
defined on a vector(sequence in general) v as: v.erase(iter) where
iter is a valid de-referenceable iterator. Clearly, v.end() isn't.
But, if we call

v.erase(v.begin(), v.end())
or
v.erase(v.end(), v.end())

will these last two calls invoke undefined behaviour ?
First case will not be undefined as v.begin() and v.end() defines a
valid range. [I am not dereferencing the end element] this will in
fact return me a v.end()

Why I am asking
is that v.clear() on an empty vector is valid - it does nothing.
v.clear() is equivalent to v.erase(v.begin(), v.end()) which in turn
is equal to v.erase(v.end(), v.end()) when 'v' is empty. Am I
correct ?
this should be okay. according to 23.1 we see that begin() == end() in
case the sequence is empty and we are still providing a valid range.
>
Kindly clarify.

Thanks
V.Subramanian
Jun 27 '08 #2

P: n/a
<su**************@yahoo.comwrote in message
news:2d**********************************@q24g2000 prf.googlegroups.com...
Consider
vector<stringv;

If we call,

v.erase(v.end())

this invokes undefined behaviour.
Correct.
But, if we call

v.erase(v.begin(), v.end())
or
v.erase(v.end(), v.end())

will these last two calls invoke undefined behaviour ?
No.

The difference is that when you call v.erase with one argument, you are
asking it to erase one element. In that case, you must specify a single
element to erase, and v.end() does not specify an element.

When you call v.erase with two arguments, you are asking it to erase all of
the elements in a range. In that case, you must specify a range, and
passing v.end() twice is one legitimate way of specifying a range that does
not contain any elements.
Jun 27 '08 #3

P: n/a
On Apr 30, 7:53 pm, "Andrew Koenig" <a...@acm.orgwrote:
<subramanian10...@yahoo.comwrote in message
news:2d**********************************@q24g2000 prf.googlegroups.com...
Consider
vector<stringv;
If we call,
v.erase(v.end())
this invokes undefined behaviour.
Correct.
But, if we call
v.erase(v.begin(), v.end())
or
v.erase(v.end(), v.end())
will these last two calls invoke undefined behaviour ?
No.
The difference is that when you call v.erase with one
argument, you are asking it to erase one element.
Couldn't one say that logically, "v.erase( i )" is the
equivalent of "v.erase( i, i + 1 )" (or whatever is necessary to
obtain an iterator one further in the container). In which
case, it's rather obvious why "v.erase( v.end() )" is illegal.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jun 27 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.