Source of set02.cpp


  1: //set02.cpp

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

  7: int main()
  8: {
  9:     cout << "\nThis program illustrates the set copy constructor and "
 10:         "the overloaded assignment\noperator for sets, as well as the "
 11:         "default constructor for the set class, and\nthe empty() member "
 12:         "function.";
 13:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 15:     cout << "\nWe begin by creating an array of 10 positive integers, "
 16:         "and\nthen we create a first set containing the same values.";
 17:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');
 18:     int a[] = {7, 4, 9, 1, 3, 8, 2, 5, 6, 10};
 19:     set<int> s1(a, a+10);

 21:     cout << "\nThe size of the set is " << s1.size() << ".";
 22:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 24:     cout << "\nHere are the values in the set:\n";
 25:     set<int>::iterator p = s1.begin();
 26:     while (p != s1.end()) cout  << *p++ << " ";
 27:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 29:     cout << "\nNext we make a copy of this set.";
 30:     set<int> s2(s1);
 31:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 33:     cout << "\nHere are the values in the copy:\n";
 34:     p = s2.begin();
 35:     while (p != s2.end()) cout  << *p++ << " ";
 36:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 38:     cout << "\nNow we create a third set using the default constructor.";
 39:     set<int> s3;
 40:     if (s3.empty()) cout << "\nThis set is empty.";
 41:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 43:     cout << "\nFinally we assign the first set to this third set.";
 44:     s3 = s1;
 45:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 47:     cout << "\nHere are the values in this third set now:\n";
 48:     p = s3.begin();
 49:     while (p != s3.end()) cout  << *p++ << " ";
 50:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');
 51: }