//---------------------------------------------------------------- // // algorithms.cpp // // Code to illustrate the use of STL algorithms. // // written by: Simon Parsons // modified : 19th June 2010 // //---------------------------------------------------------------- #include #include // Remember we need this to vectors #include // and we need this for algorithms using namespace std; //---------------------------------------------------------------- int doubleIt(int x); // We need this for the transform function int main() { int r; // Here we put random numbers into a vector vector theVector; for (int i=0; i<10; i++) { r = rand() % 100; theVector.push_back(r); } // Now use an iterator to print the elements out vector::iterator p; cout << "The vector contains: " << endl; for ( p = theVector.begin(); p != theVector.end(); p++ ) { cout << *p << " "; } cout << endl; // Now we'll illustrate some of the STL algorithms. First some that // do not modify the container: cout << "The number 86 appears: " << count(theVector.begin(), theVector.end(), 86) << " times" << endl; cout << "The number following the first occurance of 86 is: "; p = find(theVector.begin(), theVector.end(), 86); p++; cout << *p << endl; // With both functions we can specify a start and end point that are not // the first and last element of the vector. vector::iterator q; vector::iterator s; q = p; s = theVector.end() - 1; cout << "The number 86 appears " << count(q, s, 86) << " times in the subsequence" << endl; cout << "The number following the first occurance" << " of 86 in the subsequence is: "; p = find(q, s, 86); p++; cout << *p << endl; // Now some modifying reverse(theVector.begin(), theVector.end()); cout << "After reversal, we have: " << endl; for ( p = theVector.begin(); p != theVector.end(); p++ ) { cout << *p << " "; } cout << endl; // And we can do this on a subsequence: reverse(q,s); cout << "After partial re-reversal, we have: " << endl; for ( p = theVector.begin(); p != theVector.end(); p++ ) { cout << *p << " "; } cout << endl; // Now sort elements sort(theVector.begin(), theVector.end()); cout << "After sorting, we have: " << endl; for ( p = theVector.begin(); p != theVector.end(); p++ ) { cout << *p << " "; } cout << endl; // And randomly shuffle a subsequence: random_shuffle(q, s); cout << "After randomly shuffling part, we have: " << endl; for ( p = theVector.begin(); p != theVector.end(); p++ ) { cout << *p << " "; } cout << endl; // Now resorting sort(theVector.begin(), theVector.end()); cout << "After resorting, we have: " << endl; for ( p = theVector.begin(); p != theVector.end(); p++ ) { cout << *p << " "; } cout << endl; // Now rotate about a point in the list s = s - 1; rotate(theVector.begin(), theVector.end(), s); cout << "After rotating, we have: " << endl; for ( p = theVector.begin(); p != theVector.end(); p++ ) { cout << *p << " "; } cout << endl; // And finally, transform the vector: vector theOther; theOther.resize(theVector.size()); transform(theVector.begin(),theVector.end(),theOther.begin(),doubleIt); cout << "After transforming, we have: " << endl; for ( p = theOther.begin(); p != theOther.end(); p++ ) { cout << *p << " "; } cout << endl; } int doubleIt(int x){ return x *= 2; }