473,387 Members | 1,721 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,387 software developers and data experts.

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=objlist.begin();iter != objlist.end();++iter)
{
iter2 = iter;
obj = *iter;
obj->run();
if (obj->destroy_me)
{
objlist.erase(iter);
delete obj;
iter = iter2;
}
}

Thanks for any help.

B2003

Jan 22 '07 #1
10 2755

Boltar wrote:
for(iter=objlist.begin();iter != objlist.end();++iter)
{
iter2 = iter;
obj = *iter;
obj->run();
if (obj->destroy_me)
{
objlist.erase(iter);
delete obj;
iter = iter2;
}
}
Sorry , that was some old code I cut and pasted , it should have read:

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

B2003

Jan 22 '07 #2

Boltar wrote:
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=objlist.begin();iter != objlist.end();++iter)
{
iter2 = iter;
obj = *iter;
obj->run();
if (obj->destroy_me)
{
objlist.erase(iter);
delete obj;
iter = iter2;
}
}
As long as the program calls std::list's own erase() routine (as the
sample code above does) to remove the element from the list, then only
the iterator that was passed to erase() will be invalidated. In
particular, all other iterators in that same list container remain
valid and will still reference the same list element as before.

Greg

Jan 22 '07 #3
Boltar wrote:
>
Boltar wrote:
>for(iter=objlist.begin();iter != objlist.end();++iter)
{
iter2 = iter;
obj = *iter;
obj->run();
if (obj->destroy_me)
{
objlist.erase(iter);
delete obj;
iter = iter2;
}
}

Sorry , that was some old code I cut and pasted , it should have read:

for(iter=objlist.begin();iter != objlist.end();)
{
iter2 = iter
iter2++;
obj = *iter;
obj->run();
if (obj->destroy_me)
{
objlist.erase(iter);
delete obj;
iter = iter2;
}
else iter++;
}
It's not just the iterator that you used with erase() that's invalidated,
but rather all iterators to the erased element. Think about it: iter2
refers to an element that doesn't exist anymore. How could it be valid?
Hint: Have a look at the return value of erase().

Jan 22 '07 #4
Boltar wrote:
Boltar wrote:
for(iter=objlist.begin();iter != objlist.end();++iter)
{
iter2 = iter;
obj = *iter;
obj->run();
if (obj->destroy_me)
{
objlist.erase(iter);
delete obj;
iter = iter2;
}
}

Sorry , that was some old code I cut and pasted , it should have read:

for(iter=objlist.begin();iter != objlist.end();)
{
iter2 = iter
iter2++;
obj = *iter;
obj->run();
if (obj->destroy_me)
{
objlist.erase(iter);
delete obj;
iter = iter2;
}
else iter++;
}
The second iterator is not necessary. Moreover, the loop could be
streamlined a bit:

iter = objlist.begin();

while ( iter != objlist.end())
{
(*iter)->run();

if ((*iter)->destroy_me)
{
objlist.erase( iter++ );
continue;
}
iter++;
}

Greg

Jan 22 '07 #5

Rolf Magnus wrote:
Boltar wrote:
for(iter=objlist.begin();iter != objlist.end();)
{
iter2 = iter
iter2++;
obj = *iter;
obj->run();
if (obj->destroy_me)
{
objlist.erase(iter);
delete obj;
iter = iter2;
}
else iter++;
}

It's not just the iterator that you used with erase() that's invalidated,
but rather all iterators to the erased element. Think about it: iter2
refers to an element that doesn't exist anymore. How could it be valid?
No, iter2 refers to the element after the one that is erased.
Hint: Have a look at the return value of erase().
Since a std::list's iterators are stable, the iterator that
std::list::erase() will return is known before erase() is ever called.
Therefore a program can just as easily erase the element at the
iterator's current postion and advance the iterator to the next element
like so:

objlist.erase( iter++ );

Greg

Jan 22 '07 #6

"Boltar" <bo********@yahoo.co.ukwrote in message
news:11**********************@11g2000cwr.googlegro ups.com...
>
Boltar wrote:
>for(iter=objlist.begin();iter != objlist.end();++iter)
{
iter2 = iter;
obj = *iter;
obj->run();
if (obj->destroy_me)
{
objlist.erase(iter);
delete obj;
iter = iter2;
}
}

Sorry , that was some old code I cut and pasted , it should have read:

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

B2003
The "normal" way to handle this is like this:

for ( iter=objlist.begin(); iter != objlist.end(); )
{
obj->run();
if ( obj->destory_me )
iter = objlist.erase( iter );
else
++iter;
}

..erase() returns an iterator just past the one erased.
}
Jan 22 '07 #7
Greg wrote:
>
Rolf Magnus wrote:
>Boltar wrote:
for(iter=objlist.begin();iter != objlist.end();)
{
iter2 = iter
iter2++;
obj = *iter;
obj->run();
if (obj->destroy_me)
{
objlist.erase(iter);
delete obj;
iter = iter2;
}
else iter++;
}

It's not just the iterator that you used with erase() that's invalidated,
but rather all iterators to the erased element. Think about it: iter2
refers to an element that doesn't exist anymore. How could it be valid?

No, iter2 refers to the element after the one that is erased.
Ah right. I've seen that the OP corrected his code, but I was still looking
at the first version that didn't have the increment.
>Hint: Have a look at the return value of erase().

Since a std::list's iterators are stable, the iterator that
std::list::erase() will return is known before erase() is ever called.
Therefore a program can just as easily erase the element at the
iterator's current postion and advance the iterator to the next element
like so:

objlist.erase( iter++ );
Yes. However, I'd still use the return value, like:

iter = objlist.erase(iter);

Jan 22 '07 #8
"Boltar" <bo********@yahoo.co.ukwrote:
>
for(iter=objlist.begin();iter != objlist.end();)
{
iter2 = iter
iter2++;
obj = *iter;
obj->run();
if (obj->destroy_me)
{
objlist.erase(iter);
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>::iterator it = li.begin();
while ( it != li.end() )
if ( fn( *it ) )
li.erase( it++ );
}

Then you can:

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

for_each( objlist.begin(), objlist.end(), mem_fun( &MyClass::run ) );
remove_if( objlist, &should_destroy );

Or you can:

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

remove_if( objlist, &run_and_check_destroy );
Jan 22 '07 #9

Daniel T. wrote:
"Boltar" <bo********@yahoo.co.ukwrote:

for(iter=objlist.begin();iter != objlist.end();)
{
iter2 = iter
iter2++;
obj = *iter;
obj->run();
if (obj->destroy_me)
{
objlist.erase(iter);
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>::iterator it = li.begin();
while ( it != li.end() )
if ( fn( *it ) )
li.erase( it++ );
}
Actually, std::list does have a remove_if() member function.

Greg

Jan 22 '07 #10
In article <11**********************@l53g2000cwa.googlegroups .com>,
"Greg" <gr****@pacbell.netwrote:
Daniel T. wrote:
"Boltar" <bo********@yahoo.co.ukwrote:
>
for(iter=objlist.begin();iter != objlist.end();)
{
iter2 = iter
iter2++;
obj = *iter;
obj->run();
if (obj->destroy_me)
{
objlist.erase(iter);
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>::iterator 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::run ) );
objlist.remove_if( &should_destroy );
Jan 22 '07 #11

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

Similar topics

14
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...
11
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...
2
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...
3
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...
13
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...
6
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);...
2
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...
1
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'...
11
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'...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...
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,...

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.