question about erase() function on a container

  • Thread starter subramanian100in
  • Start date
S

subramanian100in

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
 
M

Martin York

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.
 
M

Martin Vuille

ps.com:
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
 
J

James Kanze

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 ?

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.
 
B

Bo Persson

Martin said:
ps.com:


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
 
A

Andrew Koenig

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,119
Latest member
IrmaNorcro
Top