home *** CD-ROM | disk | FTP | other *** search
/ Beginning C++ Through Gam…rogramming (2nd Edition) / BCGP2E.ISO / source / chapter04 / high_scores.cpp < prev   
Encoding:
C/C++ Source or Header  |  2006-09-25  |  1.2 KB  |  51 lines

  1. // High Scores
  2. // Demonstrates algorithms
  3.  
  4. #include <iostream>
  5. #include <vector>
  6. #include <algorithm>
  7. #include <ctime>
  8. #include <cstdlib>
  9.  
  10. using namespace std;
  11.  
  12. int main()
  13. {
  14.     vector<int>::const_iterator iter;
  15.  
  16.     cout << "Creating a list of scores.";
  17.     vector<int> scores;
  18.     scores.push_back(1500);
  19.     scores.push_back(3500);
  20.     scores.push_back(7500);
  21.  
  22.     cout << "\nHigh Scores:\n";
  23.     for (iter = scores.begin(); iter != scores.end(); ++iter)
  24.         cout << *iter << endl;
  25.         
  26.     cout << "\nFinding a score.";
  27.     int score;
  28.     cout << "\nEnter a score to find: ";
  29.     cin >> score;
  30.     iter = find(scores.begin(), scores.end(), score);
  31.     if (iter != scores.end())
  32.         cout << "Score found.\n";
  33.     else
  34.         cout << "Score not found.\n";
  35.  
  36.     cout << "\nRandomizing scores.";
  37.     srand(time(0));
  38.     random_shuffle(scores.begin(), scores.end());
  39.     cout << "\nHigh Scores:\n";
  40.     for (iter = scores.begin(); iter != scores.end(); ++iter)
  41.         cout << *iter << endl;
  42.  
  43.     cout << "\nSorting scores.";
  44.     sort(scores.begin(), scores.end());
  45.     cout << "\nHigh Scores:\n";
  46.     for (iter = scores.begin(); iter != scores.end(); ++iter)
  47.         cout << *iter << endl;
  48.     
  49.  return 0;
  50. }
  51.