473,320 Members | 1,724 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

remove an item in a STL list

Hello, I just want to make sure I got it right.

I have this code

list<int> L;
L.push_back(1);
L.push_back(2);
L.push_back(3);
L.push_back(4);
list<int>::iterator iter=L.begin()+1;

L.erase(iter);
L.erase(iter);

This is a problem since I can't be sure what iter is pointing to after the
first erase, right?

So I should instead do:
iter=L.erase(iter);
iter=L.erase(iter);

With a vector (V), I could get away with
V.erase(iter);
V.erase(iter);

since most likely the vector will not move around in the memory and the
deleted element will be replaced with the next one, so that iter will again
point to a valid element in the vector?

But I should use
iter=V.erase(iter);
iter=V.erase(iter);

here to, right?

Learning is fun.
Jul 23 '05 #1
9 3285
Gunnar G wrote:
Hello, I just want to make sure I got it right.

I have this code

list<int> L;
L.push_back(1);
L.push_back(2);
L.push_back(3);
L.push_back(4);
list<int>::iterator iter=L.begin()+1;
I am not sure +1 works... Recommend doing ++iter as a precaution.

L.erase(iter);
L.erase(iter);

This is a problem since I can't be sure what iter is pointing to after the
first erase, right?
Right.

So I should instead do:
iter=L.erase(iter);
iter=L.erase(iter);
I'd do

list<int>::iterator iter = L.begin();
iter = L.erase(++iter);
iter = L.erase(iter);

With a vector (V), I could get away with
V.erase(iter);
V.erase(iter);

since most likely the vector will not move around in the memory and the
deleted element will be replaced with the next one, so that iter will again
point to a valid element in the vector?
No. With a vector you should have even less confidence about iterators.
Vectors tend to reallocate at will, so you should obtain iterators after
every deletion.

But I should use
iter=V.erase(iter);
iter=V.erase(iter);

here to, right?


Right.

V
Jul 23 '05 #2
Gunnar G wrote:
Hello, I just want to make sure I got it right.

I have this code

list<int> L;
L.push_back(1);
L.push_back(2);
L.push_back(3);
L.push_back(4);
list<int>::iterator iter=L.begin()+1;

L.erase(iter);
L.erase(iter);

This is a problem since I can't be sure what iter is pointing to after the
first erase, right?

So I should instead do:
iter=L.erase(iter);
iter=L.erase(iter);

With a vector (V), I could get away with
V.erase(iter);
V.erase(iter);

since most likely the vector will not move around in the memory and the
deleted element will be replaced with the next one, so that iter will again
point to a valid element in the vector?

But I should use
iter=V.erase(iter);
iter=V.erase(iter);

here to, right?


Any iterator pointing to an element between the erased and the end of
the sequence becomes invalid. The iterator returned by erase() points to
the element immediately following the erased element. If there is no
such element, the returned iterator is invalid. Note that erase() never
triggers reallocation, thus every iterator before the removed element
will remain valid. Generally it is better not to rely on these rules and
to create new iterators after any operation that changes the sequence in
any way.
--
Regards,

Karsten
Jul 23 '05 #3
"Gunnar G" <de****@comhem.se> wrote in message
news:Si*********************@newsc.telia.net...
list<int> L;
L.push_back(1);
L.push_back(2);
L.push_back(3);
L.push_back(4);
list<int>::iterator iter=L.begin()+1;
This should not work, as list iterators are not random access iterators and
so do not support +.
L.erase(iter);
L.erase(iter);

This is a problem since I can't be sure what iter is pointing to after the
first erase, right?
Right.
So I should instead do:
iter=L.erase(iter);
iter=L.erase(iter);
Right.
With a vector (V), I could get away with
V.erase(iter);
V.erase(iter);
No you can't. Erasing an element from a vector is permitted to invalidate
all iterators referring to elements of the vector.
But I should use
iter=V.erase(iter);
iter=V.erase(iter);

here to, right?


Right.
Jul 23 '05 #4
On Tue, 08 Feb 2005 22:41:29 GMT in comp.lang.c++, "Andrew Koenig"
<ar*@acm.org> wrote,
With a vector (V), I could get away with
V.erase(iter);
V.erase(iter);


No you can't. Erasing an element from a vector is permitted to invalidate
all iterators referring to elements of the vector.


Is that a change? Last I looked it was only invalidates iterators
pointing to or after the point of the erase.
Jul 23 '05 #5
David Harmon wrote:
On Tue, 08 Feb 2005 22:41:29 GMT in comp.lang.c++, "Andrew Koenig"
<ar*@acm.org> wrote,
With a vector (V), I could get away with
V.erase(iter);
V.erase(iter);


No you can't. Erasing an element from a vector is permitted to invalidate
all iterators referring to elements of the vector.

Is that a change? Last I looked it was only invalidates iterators
pointing to or after the point of the erase.


Andrew's answer is not correct. It would be, if erase() would trigger a
reallocation, but STL reference clearly says it does not. Thus only
iterators pointing to or after the erased element become invalid.

--
Regards,

Karsten
Jul 23 '05 #6

Karsten Baumgarten wrote:
David Harmon wrote:
On Tue, 08 Feb 2005 22:41:29 GMT in comp.lang.c++, "Andrew Koenig"
<ar*@acm.org> wrote, ....
Erasing an element from a vector is permitted to invalidate
all iterators referring to elements of the vector.

Is that a change? Last I looked it was only invalidates iterators
pointing to or after the point of the erase.


Andrew's answer is not correct. It would be, if erase() would trigger

a reallocation, but STL reference clearly says it does not. Thus only
iterators pointing to or after the erased element become invalid.


In fact, because we nowadays require vector to be contiguous, 23.2.4.3
is incorrect, even in the latest draft. References must remain valid
but will point to new values, if the shrunken vector still contains
sufficient elements. E.g. erasing the second element of a 100-element
container will not invalidate a reference to the third element, simply
because the reference to the first element must remain valid.
{ &v[2]==(&v[0])+2 }

Regards,
Michiel Salters

Jul 23 '05 #7
msalters wrote:
In fact, because we nowadays require vector to be contiguous, 23.2.4.3
is incorrect, even in the latest draft. References must remain valid
but will point to new values, if the shrunken vector still contains
sufficient elements. E.g. erasing the second element of a 100-element
container will not invalidate a reference to the third element, simply
because the reference to the first element must remain valid.
{ &v[2]==(&v[0])+2 }


I just wrote some sample code and agree. But this leads to strange
results: If one erases the last element of the vector using a valid
iterator, it remains valid even though it now references an element
"outside" of the sequence. That is, it still references the - now erased
- element and happily returns it if you ask. I'd rather have my app
segfaulting instead of iterators returning "ghost elements"...

--
Regards,

Karsten
Jul 23 '05 #8
Karsten Baumgarten wrote:
[...]
I just wrote some sample code and agree. But this leads to strange
results: If one erases the last element of the vector using a valid
iterator, it remains valid even though it now references an element
"outside" of the sequence. That is, it still references the - now erased
- element and happily returns it if you ask. I'd rather have my app
segfaulting instead of iterators returning "ghost elements"...


You're jumping to wrong conclusions. Using an iterator that has become
invalid due to any operation that is said to invalidate it, produces
_undefined_behaviour_. Whatever it does is fine and you cannot expect
it to "segfault" instead of "happily returning" anything. Yes, we all
would rather prefer that our programs wrote themselves, don't we? There
are systems for which "segfaulting" is impossible. What should they do?
There is no way in C++ to require "segfaulting" because of those.

Imagine that your leg is broken and by standing on it in a certain way
you don't feel any pain. It doesn't mean it's OK to stand on it, or that
if you try standing on it again tomorrow, or somebody else tries to stand
on their broken leg, it will be exactly the same painless experience. So,
as the saying goes, DON'T DO IT(tm).

V
Jul 23 '05 #9
msalters wrote:
In fact, because we nowadays require vector to be contiguous, 23.2.4.3
is incorrect, even in the latest draft. References must remain valid
but will point to new values, if the shrunken vector still contains


No, the requirement in the standard is correct. Implementations are not
required to make sense out of references beyond the point of the erase.
While it may well be possible to do that, it's not required, and that's
what the standard says.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 23 '05 #10

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

Similar topics

23
by: Stan Cook | last post by:
I was trying to take a list of files in a directory and remove all but the ".dbf" files. I used the following to try to remove the items, but they would not remove. Any help would be greatly...
12
by: Sam Collett | last post by:
How do I remove an item with a specified value from an array? i.e. array values 1,2,2,5,7,12,15,21 remove 2 from array would return 1,5,7,12,15,21 (12 and 21 are NOT removed, duplicates are...
7
by: Kieran Simkin | last post by:
Hi all, I'm having some trouble with a linked list function and was wondering if anyone could shed any light on it. Basically I have a singly-linked list which stores pid numbers of a process's...
9
by: Merlin | last post by:
Hi, My code below doesn't work does anyone have any pointers? All my controls are programically added. Dim i As Int16 For i = 0 To Me.Controls.Count - 1 If Me.Controls(i).Name <>...
3
by: Don | last post by:
My user control has a combobox with an arraylist attached to it along with custom add and remove methods. The "Add" method is working great. However I don't understand why the "Remove" method...
2
by: Kela | last post by:
An interesting problem: I have a ListView with LabelEdit set to TRUE. When I change the label, I want to make some decisions as to whether the ListViewItem (that's just been edited) should stay in...
10
by: pamelafluente | last post by:
Hi I have a sorted list with several thousands items. In my case, but this is not important, objects are stored only in Keys, Values are all Nothing. Several of the stored objects (might be a...
1
by: dvestal | last post by:
I have a ListView with checkboxes. I want to remove items when the checkboxes are unchecked, but to do so yields an ArgumentOutOfRangeException. Is there an easy way to get around this? A...
4
by: Yansky | last post by:
Got a quick n00b question. What's the difference between del and remove?
10
by: Tony Johansson | last post by:
Hello! I have a collection with a lot of object. Each object has the following definition. public class ParametermappingObj { private string Name {get;set;} private string Id {get;set;}...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.