// algorithm example #include #include #include #include #include #include using namespace std; // function generator - void argument function returning container type int RandomNumber () { return (rand()%100); } // binary function that returns a bool bool funnyLessThan(const int& a, const int& b) { return a % 10 < b % 10; } bool lessthan10(int x) { return x < 10; } int main () { vector vec(20); list lst(20); deque deq(20); // generate generate(vec.begin(), vec.end(), RandomNumber); // copy copy(vec.begin(), vec.end(),lst.begin()); copy(vec.begin(), vec.end(),deq.begin()); cout << "The initial vector of random numbers\n"; copy(vec.begin(), vec.end(), ostream_iterator(cout," ")); cout << endl << endl; // sort sort(vec.begin(), vec.end()); sort(deq.begin(), deq.end()); // sort(lst.begin(), lst.end()); // Why doesn't this work? cout << "The vector of random numbers after the first sort\n"; copy(vec.begin(), vec.end(), ostream_iterator(cout," ")); cout << endl << endl; cout << "The deque of random numbers after the sort\n"; copy(deq.begin(), deq.end(), ostream_iterator(cout," ")); cout << endl << endl; sort(vec.begin(), vec.end(),funnyLessThan); cout << "The vector of random numbers after the second sort\n"; copy(vec.begin(), vec.end(), ostream_iterator(cout," ")); cout << endl << endl; // count cout << "count(vec.begin(), vec.end(),8) = " << count(vec.begin(), vec.end(),8) << endl; cout << "count_if(vec.begin(), vec.end(),lessthan10) = " << count_if(vec.begin(), vec.end(),lessthan10) << endl << endl; // the remove algorithm string hand{"Have a nice day"}; remove(hand.begin(),hand.end(),'a'); cout << hand << endl; hand = "Have a nice day"; string::iterator endit = remove(hand.begin(),hand.end(),'a'); hand.erase(endit,hand.end()); cout << hand << endl << endl; }