text cover

Data Abstraction and Problem Solving with C++

Walls and Mirrors

by Frank M. Carrano

Addison Wesley Logo

c09p493.cpp

Go to the documentation of this file.
00001 
00014 #include <algorithm>
00015 #include <functional>
00016 #include <ctime>
00017 #include <vector>
00018 #include <iostream>
00019 
00020 using namespace std;
00021 
00022 // number of elements in the vectors
00023 const int SIZE = 25;
00024 
00025 int main()
00026 {
00027    // create four vectors for sorting algorithms
00028    vector<int> v1;
00029    vector<int> v2(SIZE);
00030    vector<int> v3(SIZE);
00031    vector<int> v4(SIZE);
00032 
00033    // create an iterator for the vectors
00034    vector<int>::iterator iter;
00035 
00036    // seed the random number generator
00037    srand(time(0));
00038 
00039    // fill the first vector with random numbers from 1 to 50
00040    for (int i = 0; i < SIZE; i++)
00041       v1.push_back(rand() % 50);
00042 
00043    // print the original vector
00044    cout << "original vector: " << endl;
00045    for (iter = v1.begin(); iter != v1.end(); iter++)
00046       cout << *iter << " ";
00047 
00048    // copy vectors
00049    copy(v1.begin(), v1.end(), v2.begin());
00050    copy(v1.begin(), v1.end(), v3.begin());
00051    copy(v1.begin(), v1.end(), v4.begin());
00052 
00053    // perform a stable_sort on the first vector
00054    stable_sort(v1.begin(), v1.end());
00055 
00056    // print out the stable_sort vector
00057    cout << endl << "stable sort: " << endl;
00058    for (iter = v1.begin(); iter != v1.end(); iter++)
00059       cout << *iter << " ";
00060 
00061    // perform a partial_ sort on the second vector
00062    partial_sort(v2.begin(), v2.begin() + v2.size()/2, v2.end());
00063    // print out the partial_sort vector
00064    cout << endl << "partial sort to the " << v2.size()/2
00065         << "th element: " << endl;
00066    for (iter = v2.begin(); iter != v2.end(); iter++)
00067       cout << *iter << " ";
00068 
00069    // perform a nth_element sort on the third vector
00070    int n = 15;
00071    iter = v3.begin() + n;  // iterator points to the nth element
00072    nth_element(v3.begin(), iter, v3.end());
00073 
00074    // print out the nth_element vector
00075    cout << endl << "nth element sort on the " << n
00076    << "th element with nth value " << v3[n-1]
00077    << ": " << endl;
00078    for (iter = v3.begin(); iter != v3.end(); iter++)
00079       cout << *iter << " ";
00080 
00081    // perform a stable_partition sort on the fourth vector
00082    // iterator points to the first element that does not meet the
00083    // criterion - odd numbers
00084    // bind2nd is an C++ library function that creates a
00085    // unary predicate from two arguments
00086    iter = stable_partition(v4.begin(), v4.end(),
00087                            bind2nd(modulus<int>(), 2));
00088 
00089    // print out the stable_partition vector
00090    cout << endl
00091         << "stable_partition sort for odd and even numbers: "
00092         <<  "the partition element is "
00093         << *iter << ": " << endl;
00094    for (iter = v4.begin(); iter != v4.end(); iter++)
00095             cout << *iter << " ";
00096    cout << endl;
00097 
00098    return 0;
00099 }  // end main

Generated on Sun Aug 27 21:01:31 2006 for AWLogo by  doxygen 1.4.6