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
dequeinstead? - Was there a way to do it using
forinstead ofdo/whilein the inner loop?
Considering your requirements, I believe your choice of
listis good (but it’sstd::list, notstl::list).A removal from the middle of a
vectorwould invalidate all pointers, references, and iterators to elements previous to the removed one; a removal from the middle of adequewould invalidate all pointers, references, and iterators.Removal from a
list, on the other hand, is guaranteed to invalidate only iterators to the element(s) you remove, so if you take proper care when removing an element you can keep iterating forward and backward.Considering your loop, I can’t help pointing out that:
Is equivalent to the more elegant (IMO):