erasing from stl list

K

Krzysztof Poc

Hi

Is the following safe ?

list<sth>::iterator it = myList . begin();
while ( *it != myList.end() )
myList . erase ( it ++ );

great thanks for help
 
S

Saeed Amrollahi

Hi

Is the following safe ?

list<sth>::iterator it =  myList . begin();
while ( *it != myList.end() )
   myList . erase ( it ++ );

great thanks for help

Hi Krzysztof

There is a typo in your code:
while ( it != myList.end() )
I have no reason your code isn't safe, but if you want
to erase a list, there are two better ways:
MyList.erase(MyList.begin(), MyList.end());
or
MyList.clear();
Under Visual Studio 2008, the performance of erasing a range of
iterators
or to clear a list is much better. try the following code:

#include <list>
#include <iostream>

int main()
{
using namespace std;

list<int> L;
for (int i = 0; i < 1000 * 1000; ++i)
L.push_back(i);

list<int>::iterator it = L.begin();
L.erase(L.begin(), L.end()); // 1
while (*it != L.end()) // 2
L.erase(it++);
L.clear(); // 3
cout << "Done ... " << L.size() << '\n';
return 0;
}

Regards,
-- Saeed Amrollahi
 
B

Balog Pal

Krzysztof Poc said:
Is the following safe ?

list<sth>::iterator it = myList . begin();
while ( *it != myList.end() )
myList . erase ( it ++ );

Yes. What problem you suspect?
 
B

Bas

Krzysztof Poc said:
Hi

Is the following safe ?

list<sth>::iterator it = myList . begin();
while ( *it != myList.end() )
myList . erase ( it ++ );

great thanks for help

its safe.
You cannot do this with vector, though.

bas from holland
 
B

Branimir Maksimovic

Bas said:
its safe.
You cannot do this with vector, though.
vector<sth>::iterator it = myVector . begin();
while ( it != myVector.end() )
it = myVector . erase (it);

though it's not that efficient ;)

Greets
 
R

Richard Herring

Branimir Maksimovic said:
vector<sth>::iterator it = myVector . begin();
while ( it != myVector.end() )
it = myVector . erase (it);

though it's not that efficient ;)
Last time I looked, the member function clear() was both safe and
efficient for erasing all the contents of all std:: containers. Of
course, if the original question wasn't really about erasing _all_
members, it's a different matter ;-)
 
K

Krzysztof Poc

Yes. What problem you suspect?

Thanks for an answer.
My comments in the code below:

list<sth>::iterator it = myList . begin();
while ( it != myList.end() )
myList . erase ( it ++ ); // I expect this line
// to be unsafe e.g. iterator
// is invalidated before it is removed.
 
B

Balog Pal

Krzysztof Poc said:
list<sth>::iterator it = myList . begin();
while ( it != myList.end() )
myList . erase ( it ++ ); // I expect this line
// to be unsafe e.g. iterator
// is invalidated before it is removed.

The () of function call impose sequence points. So that line works as:

{
iterator curr = it;
it = it + 1;
erase(curr);
}
 
J

Jerry Coffin

[ ... ]
list<sth>::iterator it = myList . begin();
while ( it != myList.end() )
myList . erase ( it ++ ); // I expect this line
// to be unsafe e.g. iterator
// is invalidated before it is removed.

Why would you do this at all, given that once you're sure you've made
it work correctly, it's still just a less readable version of
'myList.clear();' ?
 
B

Balog Pal

Jerry Coffin said:
Why would you do this at all, given that once you're sure you've made
it work correctly, it's still just a less readable version of
'myList.clear();' ?

guess simplified for the purpose -- in normal life there is many such loops
that have that erase inside a conditionsal block.
 
K

Krzysztof Poc

[ ... ]
list<sth>::iterator it =  myList . begin();
while ( it != myList.end() )
   myList . erase ( it ++ ); // I expect this line
     // to be unsafe e.g. iterator
     // is invalidated before it is removed.

Why would you do this at all, given that once you're sure you've made
it work correctly, it's still just a less readable version of
'myList.clear();' ?

Assume the following code:

list<sth*>::iterator it = myList . begin();
while ( it != myList.end() )
{
(*it)->ReleaseResources();
myList.erase(it++);
}

If the list is long it is scanned only once. I guess that with clear
the list
would be scanned twice in the above example.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top