home *** CD-ROM | disk | FTP | other *** search
/ DOS/V Power Report 1997 May / VPR9705A.ISO / VPR_DATA / PROGRAM / CBTRIAL / SETUP / DATA.Z / SORT.CPP < prev    next >
Text File  |  1997-02-14  |  2KB  |  67 lines

  1.  #include <vector>
  2.  #include <algorithm>
  3.  #include <functional>
  4.  
  5.  using namespace std;
  6.  
  7.  struct associate
  8.  {
  9.    int num;
  10.    char chr;
  11.    associate(int n, char c) : num(n), chr(c){};
  12.    associate() : num(0), chr('\0'){};
  13.  };
  14.  
  15.  bool operator< (const associate &x, const associate &y)
  16.  {
  17.    return x.num < y.num;
  18.  }
  19.  
  20.  ostream& operator<< (ostream &s, const associate &x)
  21.  {
  22.    return s << "<" << x.num << ";" << x.chr << ">";
  23.  }
  24.  
  25.  int main ()
  26.  {
  27.    vector<associate>::iterator i, j, k;
  28.  
  29.    associate arr[20] =
  30.         {associate(-4, ' '), associate(16, ' '),
  31.          associate(17, ' '), associate(-3, 's'),
  32.          associate(14, ' '), associate(-6, ' '),
  33.          associate(-1, ' '), associate(-3, 't'),
  34.          associate(23, ' '), associate(-3, 'a'),
  35.          associate(-2, ' '), associate(-7, ' '),
  36.          associate(-3, 'b'), associate(-8, ' '),
  37.          associate(11, ' '), associate(-3, 'l'),
  38.          associate(15, ' '), associate(-5, ' '),
  39.          associate(-3, 'e'), associate(15, ' ')};
  40.    //
  41.    // Set up vectors.
  42.    //
  43.    vector<associate> v(arr+0, arr+20), v1((size_t)20), v2((size_t)20);
  44.    //
  45.    // Copy original vector to vectors #1 and #2.
  46.    //
  47.    copy(v.begin(), v.end(), v1.begin());
  48.    copy(v.begin(), v.end(), v2.begin());
  49.    //
  50.    // Sort vector #1.
  51.    //
  52.    sort(v1.begin(), v1.end());
  53.    //
  54.    // Stable sort vector #2.
  55.    //
  56.    stable_sort(v2.begin(), v2.end());
  57.    //
  58.    // Display the results.
  59.    //
  60.    cout << "Original    sort      stable_sort" << endl;
  61.    for(i = v.begin(), j = v1.begin(), k = v2.begin();
  62.        i != v.end(); i++, j++, k++)
  63.    cout << *i << "     " << *j << "     " << *k << endl;
  64.  
  65.    return 0;
  66.  }
  67.