here's how I'd do it. No need to sort yourself - just let vector.erase(...) do it.
Code:#include <vector> #include <string> #include <iostream> using namespace std; template< class T> void GetNext( vector<T> & vec, typename vector<T>::iterator& position, int step ) { for( int i = 0; i < step; ++i ) { ++position; // should be a bit more careful here about receiving .end(), but meh. if( position == vec.end() ) position = vec.begin(); } } template< class T> void DeleteEntry( vector<T> & vec, typename vector<T>::iterator& position ) { position = vec.erase(position); if( position == vec.end() ) position = vec.begin(); } template<class T> void PrintVec( vector<T> const & vec) { cout << endl; vector<T>::const_iterator it = vec.begin(); vector<T>::const_iterator end = vec.end(); for(; it != end; ++it) { cout << *it << endl; } cout << endl; } int main() { vector<string> names; names.push_back("bill"); names.push_back("sue"); names.push_back("al"); names.push_back("may"); names.push_back("gin"); names.push_back("tim"); int goose = 3; vector<string>::iterator position = names.begin(); while( names.size() ) { PrintVec( names ); GetNext( names, position, goose-1 ); DeleteEntry( names, position ); } cin.get(); }




Reply With Quote
