Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > off_the_end iterator to vector::erase() function

Reply
Thread Tools

off_the_end iterator to vector::erase() function

 
 
subramanian100in@yahoo.com, India
Guest
Posts: n/a
 
      04-30-2008
Consider
vector<string> v;

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
 
Reply With Quote
 
 
 
 
moreshwardatye@gmail.com
Guest
Posts: n/a
 
      04-30-2008
My views. Guys, correct me if I am wrong.

~Moh

On Apr 30, 9:23*pm, "(E-Mail Removed), India"
<(E-Mail Removed)> wrote:
> Consider
> vector<string> v;
>
> 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


 
Reply With Quote
 
 
 
 
Andrew Koenig
Guest
Posts: n/a
 
      04-30-2008
<(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...

> Consider
> vector<string> v;
>
> 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.


 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      05-02-2008
On Apr 30, 7:53 pm, "Andrew Koenig" <(E-Mail Removed)> wrote:
> <(E-Mail Removed)> wrote in message


> news:(E-Mail Removed)...


> > Consider
> > vector<string> v;


> > 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:(E-Mail Removed)
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
List iterator assignment fails, assert iterator not dereferencable David Bilsby C++ 5 10-09-2007 02:05 PM
What makes an iterator an iterator? Steven D'Aprano Python 28 04-20-2007 03:34 AM
Difference between Java iterator and iterator in Gang of Four Hendrik Maryns Java 18 12-22-2005 05:14 AM
How to convert from std::list<T*>::iterator to std::list<const T*>::iterator? PengYu.UT@gmail.com C++ 6 10-30-2005 03:31 AM
Iterator doubts, Decision on Iterator usage greg C++ 6 07-17-2003 01:26 PM



Advertisments