#include #include #include #include #include using namespace std; // function object class (predicate) used in call to remove_if class LessThan10 { public: bool operator() (int i) { return (i%10 != 0); } }; void main() { vector l(100); // seed pseudo-random number generator srand(time(0)); // fill list with random numbers generate(l.begin(), l.end(), rand); // remove all numbers not divisible by 10 and put iterator to end of mutated sequence in new_last vector::iterator new_last = remove_if(l.begin(), l.end(), LessThan10()); // the elements "removed" were in fact just moved to end of l. The call to erase() deletes them. l.erase(new_last, l.end()); // sort so that identical elements are consecutive sort(l.begin(), l.end()); // purge list so that only unique elements are left unique(l.begin(), l.end()); // copy all elements to standard output using ostream_iterator (described in the chapter on iterators) copy(l.begin(), l.end(), ostream_iterator(cout," ")); }