473,770 Members | 1,644 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Deleting STL list entry - are all iterators invalidated?

Hello

I need to iterate through an STL list container and delete certain
entries in it. I realise if use erase() with the iterator it will
invalidate it but what if I store it beforehand? Ie: is the code below
using a 2nd iterator variable always guaranteed to work or could there
be come internal implementation magic going on inside the list that
could prevent it?

for(iter=objlis t.begin();iter != objlist.end();+ +iter)
{
iter2 = iter;
obj = *iter;
obj->run();
if (obj->destroy_me)
{
objlist.erase(i ter);
delete obj;
iter = iter2;
}
}

Thanks for any help.

B2003

Jan 22 '07
10 2782
In article <11************ **********@l53g 2000cwa.googleg roups.com>,
"Greg" <gr****@pacbell .netwrote:
Daniel T. wrote:
"Boltar" <bo********@yah oo.co.ukwrote:
>
for(iter=objlis t.begin();iter != objlist.end();)
{
iter2 = iter
iter2++;
obj = *iter;
obj->run();
if (obj->destroy_me)
{
objlist.erase(i ter);
delete obj;
iter = iter2;
}
else iter++;
}

Something that should have been a member function of std::list:

template < typeanme T >
void remove_if( std::list<T>& li, Fn fn )
{
typename std::list<T>::i terator it = li.begin();
while ( it != li.end() )
if ( fn( *it ) )
li.erase( it++ );
}

Actually, std::list does have a remove_if() member function.
Well, there you go:

bool should_destroy( MyClass* obj ) {
if ( obj->destroy_me ) {
delete obj;
return true;
}
return false;
}

for_each( objlist.begin() , objlist.end(), mem_fun( &MyClass::ru n ) );
objlist.remove_ if( &should_dest roy );
Jan 22 '07 #11

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

Similar topics

14
5633
by: Dave | last post by:
Hello all, After perusing the Standard, I believe it is true to say that once you insert an element into a std::list<>, its location in memory never changes. This makes a std::list<> ideal for storing vertices of an arbitrary n-ary tree where a vertex contain pointers to its parent / children. These parent / child vertices need to stay put if we've got pointers to them somewhere! Am I correct in my assertion?
11
2273
by: William Payne | last post by:
Ok, in my program I have a std::list<Document*>, where Document is one of my own classes. I need to go through this list and check each item if it's ready for deletion. If it's not, skip to next, if it's ready I take steps to delete it. The thing is that the list is part of a gui application and the system removes the Document* from the list during the execution of the loop body. That means I cant use iterators to iterate over the list....
2
2546
by: Mark P | last post by:
What should be the output of the following program? The SGI STL docs say that all iterators remain valid during a splice operation. The dinkumware library reference says that iterators to spliced elts. may be invalidated. I have one system (Linux gcc) which outputs: 0 3 and another (HP aCC) which outputs:
3
9368
by: ngaloppo | last post by:
Hi, compiling a program with the code blurb below causes a runtime error ("Expression: vector iterators incompatible") due to the debug iterators in VC++ 8. The error happens in the ind_len.push_back() call, at the second iteration. I suspect that the iterator has been invalidated, but I can't see how this would be from one iteration to another in this tiny for loop. Could anyone give me more insight? Thanks!
13
6462
by: mahajan.vibhor | last post by:
I have a list of pointers. e.g A* a = new A(); // A is a class stl::list<A*list_a; I am inserting object of class in the after allocating memeory thru new operator. But when i want to erase all elements from the list. my progam crashes. I delete element by using a iterator.
6
2791
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 ());
2
1614
by: desktop | last post by:
Are there any case where iterators in a std::list gets invalidated besides from the iterator pointing to an element thats deleted? It seems that its only the std::vector that invalidates iterators pointing to elements after a deleted element since its practically just an array.
1
2540
by: subramanian100in | last post by:
Suppose I have vector<intvi; deque<intdi; list<intli; Suppose all of these containers have some elements. Suppose 'v_iter' is an iterator pointing to some element in 'vi'. Suppose 'v_beg' and 'v_end' are valid iterators pointing to some
11
4169
by: Juha Nieminen | last post by:
Assume we have this: std::list<Typelist1(10, 1), list2(20, 2); std::list<Type>::iterator iter = list1.end(); list1.swap(list2); What happens here, according to the standard? 1) 'iter' still points to list1::end(). 2) 'iter' now points to list2::end().
0
9595
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9432
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10059
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7420
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6682
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5313
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3974
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 we have to send another system
2
3578
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2822
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.