< Previous | Contents | Next >
The High Scores program creates a vector of high scores. It uses STL algorithms to search, shuffle, and sort the scores. Figure 4.5 illustrates the program.
Figure 4.5
STL algorithms search, shuffle, and sort elements of a vector of high scores.
132 Chapter 4 n The Standard Template Library: Hangman
You can download the code for this program from the Course Technology website (www.courseptr.com/downloads). The program is in the Chapter 4 folder; the filename is high_scores.cpp.
// High Scores
// Demonstrates algorithms
#include <iostream> #include <vector> #include <algorithm> #include <ctime> #include <cstdlib>
using namespace std; int main()
{
vector<int>::const_iterator iter;
cout << "Creating a list of scores."; vector<int> scores; scores.push_back(1500); scores.push_back(3500); scores.push_back(7500);
cout << "\nHigh Scores:\n";
for (iter = scores.begin(); iter != scores.end(); ++iter)
{
cout << *iter << endl;
}
cout << "\nFinding a score."; int score;
cout << "\nEnter a score to find: "; cin >> score;
iter = find(scores.begin(), scores.end(), score); if (iter != scores.end())
{
}
else
{
}
cout << "Score found.\n";
cout << "Score not found.\n";
cout << "\nRandomizing scores."; srand(static_cast<unsigned int>(time(0))); random_shuffle(scores.begin(), scores.end()); cout << "\nHigh Scores:\n";
for (iter = scores.begin(); iter != scores.end(); ++iter)
{
cout << *iter << endl;
}
cout << "\nSorting scores."; sort(scores.begin(), scores.end()); cout << "\nHigh Scores:\n";
for (iter = scores.begin(); iter != scores.end(); ++iter)
{
cout << *iter << endl;
}
return 0;
}