Source of set09.cpp


  1: //set09.cpp

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

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

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

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

 25:     cout << "\nThe number of times Dick appears in the set is "
 26:         << s.count("Dick") << ".";
 27:     cout << "\nThe number of times Bob appears in the set is "
 28:         << s.count("Bob") << ".";
 29:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');
 30: }