|
std::list<int> lstDevices;
...
...
std::list<int>::iterator iter;
std::list<int>::iterator iter2Delete;
for(iter = lstDevices.begin(); iter != lstDevices.end();)
{
iter2Delete = iter++;
lstDevices.erase(iter2Delete);
}
Use iter2Delete to save current iterator and then let iter pointer to next item. Then delete current item. Does this way have problem?
|
|
|
|
|
See here[^] for a description and sample code showing how to erase a range of elements.
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
Hi,
Thank you for your replay, after see this article, I find that I am right.
Thankyou a lot.
#include "stdafx.h"
#include <list>
#include <iostream>
int main( )
{
using namespace std;
list <int> c1;
list <int>::iterator Iter;
c1.push_back( 10 );
c1.push_back( 20 );
c1.push_back( 30 );
c1.push_back( 40 );
c1.push_back( 50 );
cout << "The initial list is:";
for ( Iter = c1.begin( ); Iter != c1.end( ); Iter++ )
cout << " " << *Iter;
cout << endl;
std::list<int>::iterator iter;
std::list<int>::iterator iter2Delete;
for(iter = c1.begin(); iter != c1.end();)
{
iter2Delete = iter++;
c1.erase(iter2Delete);
}
for (Iter = c1.begin( ); Iter != c1.end( ); Iter++ )
cout << " " << *Iter;
cout << endl;
}
|
|
|
|
|
Why not use the features of the class and do it the easy way?
c1.erase( c1.begin(), c1.end( ) );
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
Or just this:
c1.clear();
Steve
|
|
|
|