473,473 Members | 1,782 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

question about erase() function on a container

Suppose I have

vector<intcontainer;

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
Feb 3 '08 #1
5 1455
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/s...tor/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.

Feb 3 '08 #2
Martin York <Ma***************@gmail.comwrote in
news:14**********************************@j78g2000 hsd.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/s...tor/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.
Feb 3 '08 #3
su**************@yahoo.com, India wrote:
Suppose I have
vector<intcontainer;
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.

--
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
Feb 3 '08 #4
Martin Vuille wrote:
Martin York <Ma***************@gmail.comwrote in
news:14**********************************@j78g2000 hsd.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/s...tor/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


Feb 3 '08 #5
"Bo Persson" <bo*@gmb.dkwrote in message
news:60*************@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().
Feb 3 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Generic Usenet Account | last post by:
To settle the dispute regarding what happens when an "erase" method is invoked on an STL container (i.e. whether the element is merely removed from the container or whether it also gets deleted in...
5
by: Angus Leeming | last post by:
Dinkumware's online STL reference http://tinyurl.com/3es52 declares std::map's overloaded erase member functions to have the interface: map::erase iterator erase(iterator where); iterator...
7
by: jose luis fernandez diaz | last post by:
Hi, Is this right any stl container (vector, deque, list, . . .)? typedef vector container; int main() { container<int> c1;
10
by: Pat | last post by:
Hi, I use "vector<int> v" in my program. If "v" contains the following: (front) 1 2 3 4 2 5 7 1 (end), I want to remove some element, say "2", and I want the resultant "v" to be as follows:...
21
by: Jon Slaughter | last post by:
I have a class that is basicaly duplicated throughout several files with only members names changing according to the class name yet with virtually the exact same coding going on. e.g. class...
10
by: Piotr | last post by:
In Effective STL item 9 "Choose carefully among erasing options", it has this example: bool badValue(int x); // returns whether x is 'bad' c.erase ( remove_if(c.begin(), c.end(), badValue),...
8
by: olanglois | last post by:
Hi, I was asking myself to following question. What is better to erase an element from a STL map: calling (option #1) size_type erase(const key_type& k) or calling (option #2)
3
by: noone | last post by:
string operator()(const bool clean=true) { string rv; MPEGQUEUE::reverse_iterator i=thequeue.rbegin(); MPEGQUEUE::reverse_iterator t=thequeue.rend(); while (i!=thequeue.rend()) { if...
0
by: subramanian100in | last post by:
Suppose I have vector<intc; for (int i = 0; i < 10; ++i) c.push_back(i); vector<int>::iterator it = find(c.begin(), c.end(), 5); If I do, c.insert(c.begin(), 10);
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.