Source of pair1.cpp


  1: //pair1.cpp

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

  8: int main()
  9: {
 10:     cout << "\nThis program illustrates the pair template struct.";
 11:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 13:     cout << "\nFirst we use the two-parameter pair constructor to create "
 14:         "two pairs:\na char/int pair and a string/double pair. Then we "
 15:         "display the first\nand second components of each.";
 16:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 18:     pair<char, int> pair1('A', 65);
 19:     pair<string, double> pair2("John", 99.95);
 20:     cout << "\nThe char/int pair:      "
 21:         << pair1.first << " " << pair1.second;
 22:     cout << "\nThe string/double pair: "
 23:         << pair2.first << " " << pair2.second;
 24:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 26:     cout << "\nNext we create three empty pair objects using the default "
 27:         "constructor for\neach: a char/int pair, a string/double pair, "
 28:         "and an int/int pair. Then we\ncreate appropriate pair objects and "
 29:         "assign them to these empty objects.\nThe first two are created "
 30:         "using the make_pair function, and the last is\ncreated using "
 31:         "the two-parameter constructor once again.";
 32:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');
 33:     pair<char, int> pair3;
 34:     pair<string, double> pair4;
 35:     pair<int, int> pair5;
 36:     pair3 = make_pair('B', 66);
 37:     pair4 = make_pair("Jack", 99.99);
 38:     pair5 = pair<int, int>(25, 77);
 39:     cout << "\nThe char/int pair:      "
 40:         << pair3.first << " " << pair3.second;
 41:     cout << "\nThe string/double pair: "
 42:         << pair4.first << " " << pair4.second;
 43:     cout << "\nThe int/int pair:       "
 44:         << pair5.first << " " << pair5.second;
 45:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 47:     cout << "\nFinally, we use the copy constructor to create copies "
 48:         "of two pairs used\npreviously, and then display them once again.";
 49:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 51:     pair<char, int> pair6(pair3);
 52:     pair<string, double> pair7 = pair4;
 53:     cout << "\nA char/int pair seen above:      "
 54:         << pair6.first << " " << pair6.second;
 55:     cout << "\nA string/double pair seen above: "
 56:         << pair7.first << " " << pair7.second;
 57:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');
 58: }