473,563 Members | 2,668 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

list erase without invalidating iterator

Is there a way to call erase(iter) on a list without invalidating the
iterator? Can I make a copy of an iterator and then move forward the
original without moving the copy? I'm aware of the existence of
remove_if, but in the case I'm dealing with it would be much more
natural to use an iterator.

Dec 2 '06 #1
6 11883
ca************@ gmail.com wrote:
Is there a way to call erase(iter) on a list without invalidating the
iterator? Can I make a copy of an iterator and then move forward the
original without moving the copy? I'm aware of the existence of
remove_if, but in the case I'm dealing with it would be much more
natural to use an iterator.
Probably what you want is one of:

erase( iter++);
erase( iter--);
Dec 2 '06 #2
On 1 Dec 2006 18:46:59 -0800 in comp.lang.c++, ca************@ gmail.com
wrote,
>Is there a way to call erase(iter) on a list without invalidating the
iterator?
No. The iterator points to the element you are erasing; afterwards the
element is gone. You can't have a valid iterator pointing to a gone
element.
Can I make a copy of an iterator and then move forward the
original without moving the copy?
Sure, but remember that erase() returns the next iterator value, which
is probably what you want to use.
Dec 2 '06 #3
<ca************ @gmail.comwrote in message
news:11******** *************@7 3g2000cwn.googl egroups.com...
Is there a way to call erase(iter) on a list without invalidating the
iterator? Can I make a copy of an iterator and then move forward the
original without moving the copy? I'm aware of the existence of
remove_if, but in the case I'm dealing with it would be much more
natural to use an iterator.
iter = MyContainer.era se(whatever);

iter will automatcially get incremented to the next element. So you don't
want to increment it in your for loop. Algorithm I use is:

for ( MyContainerType ::iterator it = MyContainer.beg in(); it !=
MyContainer.end (); )
{
if ( (*it).somecondi tion() == somethingelse )
it = MyContainer.era se( it );
else
++it;
}
Dec 2 '06 #4

"Mark P" <us****@fall200 5REMOVE.fastmai lCAPS.fmwrote in message
news:O_******** *********@newss vr21.news.prodi gy.net...
ca************@ gmail.com wrote:
>Is there a way to call erase(iter) on a list without invalidating the
iterator? Can I make a copy of an iterator and then move forward the
original without moving the copy? I'm aware of the existence of
remove_if, but in the case I'm dealing with it would be much more
natural to use an iterator.

Probably what you want is one of:

erase( iter++);
erase( iter--);
That's not going to work is it? I think that incrementing in invalidated
iterator is just as invalid as using it again.

But the erase function returns an iterator you can use after the erase
(pointing to the next item), so just do:

iter = erase(iter);

-Howard
Dec 4 '06 #5
Howard wrote:
"Mark P" <us****@fall200 5REMOVE.fastmai lCAPS.fmwrote in message
news:O_******** *********@newss vr21.news.prodi gy.net...
>ca************@ gmail.com wrote:
>>Is there a way to call erase(iter) on a list without invalidating
the iterator? Can I make a copy of an iterator and then move
forward the original without moving the copy? I'm aware of the
existence of remove_if, but in the case I'm dealing with it would
be much more natural to use an iterator.

Probably what you want is one of:

erase( iter++);
erase( iter--);

That's not going to work is it? I think that incrementing in
invalidated iterator is just as invalid as using it again.
Nope. The iterator [still valid] is incremented just before the old
value (copied into a temporary object) is passed to the 'erase' function.
At the time of the function taking the necessary steps to remove the
element to which the iterator (passed into the function) points, the
iterator outside the function ('iter' variable) is pointing to the next
element. And that iterator is not going to be invalidated during the
erasure operation.
But the erase function returns an iterator you can use after the erase
(pointing to the next item), so just do:

iter = erase(iter);
Yes. It doesn't change the fact that 'erase(iter++)' is a valid way
to do it, though.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Dec 4 '06 #6

"Victor Bazarov" <v.********@com Acast.netwrote in message
news:el******** **@news.datemas .de...
Howard wrote:
>"Mark P" <us****@fall200 5REMOVE.fastmai lCAPS.fmwrote in message
news:O_******* **********@news svr21.news.prod igy.net...
>>ca************@ gmail.com wrote:
Is there a way to call erase(iter) on a list without invalidating
the iterator? Can I make a copy of an iterator and then move
forward the original without moving the copy? I'm aware of the
existence of remove_if, but in the case I'm dealing with it would
be much more natural to use an iterator.
Probably what you want is one of:

erase( iter++);
erase( iter--);

That's not going to work is it? I think that incrementing in
invalidated iterator is just as invalid as using it again.

Nope. The iterator [still valid] is incremented just before the old
value (copied into a temporary object) is passed to the 'erase' function.
At the time of the function taking the necessary steps to remove the
element to which the iterator (passed into the function) points, the
iterator outside the function ('iter' variable) is pointing to the next
element. And that iterator is not going to be invalidated during the
erasure operation.
Ah, ok.
>
>But the erase function returns an iterator you can use after the erase
(pointing to the next item), so just do:

iter = erase(iter);

Yes. It doesn't change the fact that 'erase(iter++)' is a valid way
to do it, though.
Cool! Thanks, Victor.
-Howard
Dec 4 '06 #7

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

Similar topics

13
20481
by: Paras | last post by:
Hi What is the correct way to delete an element from STL list while iterating through the list list<A*> _ls; A * a; list<A*>::iterator si1;
2
9953
by: s | last post by:
Here is a snippet of my code: <code> list< MyClass * >outgoing_pool; MyClass* GetWaitingObject(string freq) { MyClass *temp_ptr = NULL; list<MyClass*>::iterator i;
3
3077
by: Kenneth Massey | last post by:
Is the following code guaranteed to work? I have a hashed set of pointers, which I would like to selectively delete from. Can I iterate through the set, deleting certain ones, without invalidating the iterator? This ominous statement in the STL "set" documentation makes me worried. I saw no similar statement on the "hash_multiset" docs. ...
7
1762
by: John Smith | last post by:
Today I experienced something very very weird with STL. The code snippet below should work, but it doesn't with Microsoft VC++ 6.0. Ok what I have is a list of network connections. The code below removes one of these connections from a temporary list. It goes through all the items to find the right connection and removes it by erasing with...
3
6092
by: Dalbosco J-F | last post by:
Hi, Sorry if this has already been answered. Given a std::list and a reverse_iterator is there a way to erase the element pointed to by the reverse_iterator via the erase method? Apparently the erase method takes a normal iterator and it fails to compile if I pass it a reverse_iterator. On the other hand, I don't wan't to use...
6
2765
by: Jakob Bieling | last post by:
Hi, I want to move an element from a std::list to the end of the same list. To get this done, I thought I'd just do something like: std::list <intlst; lst.push_back (0); lst.push_back (1); lst.push_back (2); lst.splice (lst.end (), lst, lst.begin (), ++ lst.begin ());
4
3888
by: TBass | last post by:
Hi, I've got a class that uses a std::list to store tags that need to be written out to a device. I've had no problem with the lists in terms of iterating and reading values. My problem is when I need to pop that value off the list when I'm done with it. In this case, I think the iterator is getting "lost" because I pop off the value it...
9
6698
by: Krice | last post by:
I get "list erase iterator outside range" message from MSVC with this function: int Level::Remove_Npc(Game_Object *o) { list <Game_Object *>::iterator pos; Game_Object *c; for (pos=npc_list.begin(); pos!=npc_list.end(); pos++) {
0
7659
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7882
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8103
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7634
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6244
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5481
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5208
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
1194
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
916
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.