A bit of an elementary question, I'm looking for an STL container suited to the following types of uses:
1) Supports regular forward iteration
2) Supports deletion from middle of list while walking backwards through the list, and
3) If something gets deleted while walking backwards, I need to stop and walk forwards to the end of the list.
I used a regular std::list
and found walking forward from a point in the list to be a little difficult, but doable. I used a combination of forward and reverse_iterator, and managed to avoid using advance
(because that would be expensive!)
Here is my code.
#include <list>
using namespace std ;
int main()
{
list< int > os ;
os.push_back( 1 ) ;
os.push_back( 2 ) ;
os.push_back( 3 ) ;
os.push_back( 4 ) ;
os.push_back( 5 ) ;
// 1 2 3 4 5
// should print:
// 5: NOTHING
// 4: 5
// 3: 4,5
// 2: 3,4,5
// 1: 2,3,4,5
for( list<int>::reverse_iterator riter = os.rbegin() ; riter != os.rend() ; ++riter )
{
//printf( "All the ones in FRONT of %d are:\n", riter->a ) ;
printf( "%d: ", *riter ) ;
// You can't do it with a for loop.
//for( list<O>::reverse_iterator iter = riter ; iter != os.rbegin() ; --iter )
list<int>::reverse_iterator iter = riter ;
if( iter != os.rbegin() ) do
{
--iter ; // move the iterator back.
printf( " %d", *iter ) ;
} while ( iter != os.rbegin() ) ;
//else printf( " NOTHING AFTER ME" ) ;
puts("");
}
}
My questions are:
- Have I made a bad choice of container? Should I have used
deque
instead? - Was there a way to do it using
for
instead ofdo/while
in the inner loop?