Source of set12.cpp


  1: //set12.cpp

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

  8: int main()
  9: {
 10:     cout << "\nThis program illustrates the clear() and erase() member "
 11:         "functions\nof the set interface.";
 12:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 14:     //Create an array of names
 15:     string a[] =
 16:     {
 17:         "Alice",
 18:         "Bob",
 19:         "Carl",
 20:         "Dick",
 21:         "Eve",
 22:         "Fred",
 23:         "George",
 24:         "Harry",
 25:         "Ian",
 26:         "Jack"
 27:     };
 28:     cout << "\nWe begin with a set containing the following names:\n";
 29:     set<string> s(a, a+10);
 30:     set<string>::iterator p = s.begin();
 31:     while (p != s.end()) cout << *p++ << endl;
 32:     cout << "Press Enter to continue ... ";  cin.ignore(80, '\n');

 34:     cout << "\nThen we delete George.";
 35:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 37:     set<string>::size_type numberDeleted = s.erase("George");
 38:     cout << "\nThe number of deletions was " << numberDeleted << ".";
 39:     cout << "\nAnd here are the remaining contents of the set:\n";
 40:     p = s.begin();
 41:     while (p != s.end()) cout << *p++ << endl;
 42:     cout << "Press Enter to continue ... ";  cin.ignore(80, '\n');

 44:     cout << "\nNext we delete William.";
 45:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 47:     numberDeleted = s.erase("William");
 48:     cout << "\nThe number of deletions was " << numberDeleted << ".";
 49:     cout << "\nAnd here are the remaining contents of the set:\n";
 50:     p = s.begin();
 51:     while (p != s.end()) cout << *p++ << endl;
 52:     cout << "Press Enter to continue ... ";  cin.ignore(80, '\n');

 54:     cout << "\nNow we delete the first name in the set, using an iterator "
 55:         "for access.";
 56:     s.erase(s.begin());
 57:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');
 58:     cout << "\nThe remaining names in the set are now these:\n";
 59:     p = s.begin();
 60:     while (p != s.end()) cout << *p++ << endl;
 61:     cout << "Press Enter to continue ... ";  cin.ignore(80, '\n');

 63:     cout << "\nFor our final deletion before clearing all the remaining "
 64:         "\npairs, we delete everyone in the \"range\" from Carl to Ian.";
 65:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 67:     s.erase(s.find("Carl"), s.find("Ian"));
 68:     cout << "\nAfter deleting this range of names, the remaining names "
 69:         "in the set are:\n";
 70:     p = s.begin();
 71:     while (p != s.end()) cout << *p++ << endl;
 72:     cout << "Press Enter to continue ... ";  cin.ignore(80, '\n');

 74:     cout << "\nFinally, we clear all remaining values from the set.";
 75:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 77:     s.clear();
 78:     if (s.empty()) cout << "\nThe set is now empty.";
 79:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');
 80: }