Source of set10.cpp


  1: //set10.cpp

  3: #include <iostream>
  4: #include <set>
  5: #include <string>
  6: using namespace std;

  8: int main()
  9: {
 10:     cout << "\nThis program illustrates the found() member function from "
 11:         "the set class.";
 12:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 14:     cout << "\nFirst we create an array of name strings, and then a set "
 15:         "of name strings\nfrom that array.";
 16:     string a[] = {"Tom", "Dick", "Harry", "Alice"};
 17:     set<string> setOfNames(a, a+4);
 18:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 20:     cout << "\nHere are the " << setOfNames.size() << " names in the set."
 21:         "\nCompare their order here with their order in the array:\n";
 22:     set<string>::iterator p = setOfNames.begin();
 23:     while (p != setOfNames.end()) cout << *p++ << " ";
 24:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 26:     cout << "\nNow you can search for any name in the set.";
 27:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 29:     string nameToFind;
 30:     cout << "\nEnter a name, or press Enter to quit: ";
 31:     getline(cin, nameToFind);
 32:     while (nameToFind != "")
 33:     {
 34:         p = setOfNames.find(nameToFind);
 35:         if (p != setOfNames.end())
 36:             cout << "Found " << nameToFind << ".\n";
 37:         else
 38:             cout << nameToFind << " was not found.\n";
 39:         cout << "\nEnter another name, or press Enter to quit: ";
 40:         getline(cin, nameToFind);
 41:     }
 42:     cout << "Press Enter to continue ... ";  cin.ignore(80, '\n');
 43: }