question about erase() function on a container



 DEVELOP > c-Plus-Plus > question about erase() function on a container

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "India"
Date: 02 Feb 2008 08:36:53 PM
Object: question about erase() function on a container
Suppose I have
vector<int> container;
Suppose I store some values into "container".
Suppose that "left" and "right" are valid iterators into "container"
and that "right" is NOT container.end() and "right" comes after
"left".
Given this, suppose
vector<int>::iterator iter = container.erase(left, right);
then "iter" will always be a copy of "right" iterator.
ie
if (iter == right) will always be true. Am I correct ? The same is
true for deque and list also. Is this correct ?
Given a standard library container, can we call clear() function on
empty container ? Or will it invoke undefined behaviour ?
Kindly clarify.
Thanks
V.Subramanian
.

User: "Andrew Koenig"

Title: Re: question about erase() function on a container 03 Feb 2008 11:11:49 AM
<subramanian100in@yahoo.com> wrote in message
news:feb0520c-3f4e-43c0-b375-f92bf64e2e88@v46g2000hsv.googlegroups.com...

Suppose I have
vector<int> container;
Suppose I store some values into "container".
Suppose that "left" and "right" are valid iterators into "container"
and that "right" is NOT container.end() and "right" comes after
"left".
Given this, suppose
vector<int>::iterator iter = container.erase(left, right);
then "iter" will always be a copy of "right" iterator.
ie
if (iter == right) will always be true. Am I correct ? The same is
true for deque and list also. Is this correct ?

Not quite.
For a vector or a dque, calling erase invalidates "right", so you can't talk
about "iter" being a copy of "right." However, the concept is right --
after calling erase, "iter" will refer to the position that corresponds to
where "right" referred before the erase.
We can make this statement more formal as follows:
// the number of elements we intend to erase
vector<int>::size_type count = right - left;
// the index that corresponds to "right"
vector<int>::size_type rightpos = right - container.begin();
vector<int>::iterator iter = container.erase(left, right);
assert (iter - container.begin() + count == rightpos);
In other words, after the erase, "iter" will refer to an index that is
"count" positions before the index to which "right" referred before the
erase.
For a list, the value of "iter" after the erase will be the same as the
value of "right"

Given a standard library container, can we call clear() function on
empty container ? Or will it invoke undefined behaviour ?

Yes, we can call clear() on an empty container. It doesn't do anything.
.

User: "Martin York"

Title: Re: question about erase() function on a container 02 Feb 2008 08:42:47 PM

Suppose that "left" and "right" are valid iterators into "container"
and that "right" is NOT container.end() and "right" comes after
"left".

vector<int>::iterator iter = container.erase(left, right);

then "iter" will always be a copy of "right" iterator.

No. iter will be an iterator to the element after right:
see: http://www.cplusplus.com/reference/stl/vector/erase.html

Given a standard library container, can we call clear() function on
empty container ? Or will it invoke undefined behaviour ?

Yes. clear() will do what is expected on an empty container.
.
User: "Martin Vuille"

Title: Re: question about erase() function on a container 02 Feb 2008 09:36:11 PM
Martin York <Martin.YorkAmazon@gmail.com> wrote in
news:146ae33b-9ed0-4641-8bc0-875e2fc71eb7@j78g2000hsd.googlegrou
ps.com:


Suppose that "left" and "right" are valid iterators into
"container" and that "right" is NOT container.end() and
"right" comes after "left".

vector<int>::iterator iter = container.erase(left, right);

then "iter" will always be a copy of "right" iterator.


No. iter will be an iterator to the element after right:
see: http://www.cplusplus.com/reference/stl/vector/erase.html

Which says: "the range includes all the elements between first and
last, including the element pointed by first but *not* the one
pointed by last." (emphasis mine)
iter will point to the same element as right. But the two
iterators will not be "equal" or "copies" because erase()
invalidates all iterators to elements after position first.
Thus right is no longer valid after erase() returns.
MV
--
I do not want replies; please follow-up to the group.
.
User: "Bo Persson"

Title: Re: question about erase() function on a container 03 Feb 2008 06:36:40 AM
Martin Vuille wrote:

Martin York <Martin.YorkAmazon@gmail.com> wrote in
news:146ae33b-9ed0-4641-8bc0-875e2fc71eb7@j78g2000hsd.googlegrou
ps.com:


Suppose that "left" and "right" are valid iterators into
"container" and that "right" is NOT container.end() and
"right" comes after "left".

vector<int>::iterator iter = container.erase(left, right);

then "iter" will always be a copy of "right" iterator.


No. iter will be an iterator to the element after right:
see: http://www.cplusplus.com/reference/stl/vector/erase.html


Which says: "the range includes all the elements between first and
last, including the element pointed by first but *not* the one
pointed by last." (emphasis mine)

iter will point to the same element as right. But the two
iterators will not be "equal" or "copies" because erase()
invalidates all iterators to elements after position first.
Thus right is no longer valid after erase() returns.

And that is a very strong reason for erase() to return a new, valid
iterator!
Bo Persson
.
User: "Andrew Koenig"

Title: Re: question about erase() function on a container 03 Feb 2008 11:12:38 AM
"Bo Persson" <bop@gmb.dk> wrote in message
news:60lqp8F1rcdouU1@mid.individual.net...

iter will point to the same element as right. But the two
iterators will not be "equal" or "copies" because erase()
invalidates all iterators to elements after position first.
Thus right is no longer valid after erase() returns.

And that is a very strong reason for erase() to return a new, valid
iterator!

If you need such an iterator, you can call begin() or end().
.




User: "James Kanze"

Title: Re: question about erase() function on a container 03 Feb 2008 04:45:42 AM
subramanian100in@yahoo.com, India wrote:

Suppose I have
vector<int> container;
Suppose I store some values into "container".
Suppose that "left" and "right" are valid iterators into
"container" and that "right" is NOT container.end() and
"right" comes after "left".
Given this, suppose
vector<int>::iterator iter =3D container.erase(left, right);
then "iter" will always be a copy of "right" iterator.
ie
if (iter =3D=3D right) will always be true. Am I correct ? The same is
true for deque and list also. Is this correct ?

No.
After the erase, right is invalid, and any attempt to use it
(including comparing it with iter) is undefined behavior. With
g++, in debug mode, it crashes---I would expect this to be the
case with any quality implementation.

Given a standard library container, can we call clear() function on
empty container ? Or will it invoke undefined behaviour ?

You can always call clear(), on any container.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34
.


  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER