Source of set05.cpp


  1: //set05.cpp

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

  7: int main()
  8: {
  9:     cout << "\nThis program illustrates the construction of a set from "
 10:         "an array of pairs.\nThe set is then displayed in two different "
 11:         "ways. The code illustrates the\nuse of  the operators =, !=, "
 12:         "++, --, *, -> and == with set iterators.";
 13:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 15:     //Create an array of pairs
 16:     pair<char, int> a[] =
 17:     {
 18:         pair<char, int>('C', 67),
 19:         pair<char, int>('A', 65),
 20:         pair<char, int>('E', 69),
 21:         pair<char, int>('D', 68),
 22:         pair<char, int>('B', 66)
 23:     };
 24:     //Initialize a set with values from the array
 25:     set<pair<char, int>> s(a, a+5);

 27:     cout << endl;
 28:     set<pair<char, int>>::iterator p;
 29:     for (p = s.begin(); p != s.end(); ++p)
 30:         cout << p->first << " has an ASCII code of " << p->second << ".\n";
 31:     if (p == s.end())
 32:         cout << "Our iterator is now pointing to one-past-the-last position.";
 33:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 35:     p = s.end();
 36:     cout << endl;
 37:     while (p != s.begin())
 38:     {
 39:         --p;
 40:         cout << p->first << " has an ASCII code of " << p->second << ".\n";
 41:     }
 42:     if (p == s.begin())
 43:         cout << "Our iterator is now pointing to the first position.";
 44:     if (*p == *s.begin())
 45:         cout << "\nWe confirm this by checking component equality as well.";
 46:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');
 47: }