Source of set16.cpp


  1: //set16.cpp

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

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

 13:     //Create an array of integers
 14:     int a[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
 15:     set<int> s1(a, a+3);
 16:     set<int> s2(a+3, a+10);

 18:     cout << "\nWe begin by creating two sets.";
 19:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 21:     cout << "\nThe first set has size " << s1.size() << " and contains the "
 22:         "following integers:\n";
 23:     set<int>::iterator p = s1.begin();
 24:     while (p != s1.end()) cout << *p++ << " ";
 25:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 27:     cout << "\nThe second set has size " << s2.size() << " and contains the "
 28:         "following integers:\n";
 29:     p = s2.begin();
 30:     while (p != s2.end()) cout << *p++ << " ";
 31:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 33:     cout << "\nNow we swap the two sets using the swap() function "
 34:         "from the set interface.";
 35:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 37:     s1.swap(s2);

 39:     cout << "\nAfter the swap the first set has size " << s1.size()
 40:         << " and contains the following integers:\n";
 41:     p = s1.begin();
 42:     while (p != s1.end()) cout << *p++ << " ";
 43:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 45:     cout << "\nAnd the second set has size " << s2.size()
 46:         << " and contains the following integers:\n";
 47:     p = s2.begin();
 48:     while (p != s2.end()) cout << *p++ << " ";
 49:     cout << "Press Enter to continue ... ";  cin.ignore(80, '\n');
 50: }