1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
#include <iostream>
#include <list>
// advance the iterator by skipping over n elements or getting to the end
// return true if end was not reached
template < typename ITERATOR > bool skip( ITERATOR& iter, std::size_t n, ITERATOR end )
{
while( n-- && iter != end ) ++iter ;
return iter != end ;
}
int main()
{
std::list<int> lst { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17 } ;
auto it = lst.begin() ; // erase every 4th element (skip 3 elements after each erase)
while( skip( it, 3, lst.end() ) ) it = lst.erase(it) ;
for( int v : lst ) std::cout << v << ' ' ;
std::cout << '\n' ;
}
|