Source of list_b.cpp


  1: //list_b.cpp

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

  8: int main()
  9: {
 10:     cout << "\nThis program illustrates how to display the name of "
 11:         "the underlying type\nrepresented by any typedef defined "
 12:         "by a list class. To get a good\nfeel for what this "
 13:         "program shows you, you should change the meaning of"
 14:         "\nTrialType a couple of times, re-building and re-running "
 15:         "after each change.\nYou will also need to change the call "
 16:         "to the random generator that produces\nthe component values "
 17:         "of the list.";
 18:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 20:     //Experiment by changing the definition of TrialType.
 21:     //typedef int TrialType;
 22:     //typedef double TrialType;
 23:     typedef char TrialType;
 24:     TrialType a[] = {(TrialType)65,
 25:                      (TrialType)67,
 26:                      (TrialType)69,
 27:                      (TrialType)71,
 28:                      (TrialType)73};
 29:     list<TrialType> lst(a, a+5);
 30:     cout << "\nSize of lst = " << lst.size();
 31:     cout << "\nContents of lst: ";
 32:     list<TrialType>::iterator p = lst.begin();
 33:     while (p != lst.end()) cout << *p++ << " ";
 34:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 36:     list<TrialType>::size_type i;
 37:     i = lst.max_size(); //Typical assignment to i
 38:     const char* dataType1 = typeid(i).name();
 39:     cout << "\nlist<TrialType>::size_type\nis equivalent "
 40:         "to the following type: " << dataType1
 41:         << "\n\nIf i is of type list<TrialType>::size_type "
 42:         "and\ni = lst.max_size();\nthen the value of i is "
 43:         << static_cast<unsigned int>(i) << ", for the given lst.\n";
 44:     cout << "Press Enter to continue ... ";  cin.ignore(80, '\n');


 47:     const char* dataType2 = 
 48:         typeid(list<TrialType>::difference_type).name();
 49:     cout << "\nlist<TrialType>::difference_type\nis equivalent "
 50:         "to the following type: " << dataType2 << endl;
 51:     cout << "Press Enter to continue ... ";  cin.ignore(80, '\n');


 54:     const char* dataType3 = typeid(list<TrialType>::reference).name();
 55:     cout << "\nlist<TrialType>::reference\nis equivalent "
 56:         "to the following type: " << dataType3 << endl;
 57:     cout << "Press Enter to continue ... ";  cin.ignore(80, '\n');

 59:     const char* dataType4 =
 60:         typeid(list<TrialType>::const_reference).name();
 61:     cout << "\nlist<TrialType>::const_reference\nis equivalent "
 62:         "to the following type: " << dataType4 << endl;
 63:     cout << "Press Enter to continue ... ";  cin.ignore(80, '\n');


 66:     list<TrialType>::iterator itr;
 67:     itr =  lst.begin();  ++itr;  ++itr;
 68:     const char* dataType5 = typeid(itr).name();
 69:     cout << "\nlist<TrialType>::iterator\nis equivalent to the "
 70:         "following type:\n\n" << dataType5
 71:         << "\nIf itr is of type list<TrialType>::iterator "
 72:         "and itr points at the 3rd value,\nthen the value pointed to "
 73:         "by itr is " << *itr << ", for the given lst.\n";
 74:     cout << "Press Enter to continue ... ";  cin.ignore(80, '\n');

 76:     list<TrialType>::const_iterator itr_c;
 77:     itr_c = lst.begin();  ++itr_c;
 78:     const char* dataType6 = typeid(itr_c).name();
 79:     cout << "\nlist<TrialType>::const_iterator\nis equivalent "
 80:         "to the following type:\n" << dataType6
 81:         << "\nIf itr_c is of type list<TrialType>::const_iterator "
 82:         "and itr_c points at\nthe second value, then the value "
 83:         "pointed to by itr_c is " << *itr_c << ", for the given lst.\n";
 84:     cout << "Press Enter to continue ... ";  cin.ignore(80, '\n');


 87:     const char* dataType7 =
 88:         typeid(list<TrialType>::reverse_iterator).name();
 89:     cout << "\nlist<TrialType>::reverse_iterator\nis equivalent "
 90:         "to the following type:\n" << dataType7 << endl;
 91:     cout << "Press Enter to continue ... ";  cin.ignore(80, '\n');

 93:     const char* dataType8 =
 94:         typeid(list<TrialType>::const_reverse_iterator).name();
 95:     cout << "\nlist<TrialType>::const_reverse_iterator\nis equivalent "
 96:         "to the following type:\n" << dataType8 << endl;
 97:     cout << "Press Enter to continue ... ";  cin.ignore(80, '\n');


100:     const char* dataType9 = typeid(list<TrialType>::pointer).name();
101:     cout << "\nlist<TrialType>::pointer\nis equivalent "
102:         "to the following type: " << dataType9 << endl;
103:     cout << "Press Enter to continue ... ";  cin.ignore(80, '\n');

105:     const char* dataType10 =
106:     typeid(list<TrialType>::const_pointer).name();
107:     cout << "\nlist<TrialType>::const_pointer\nis equivalent "
108:         "to the following type: " << dataType10 << endl;
109:     cout << "Press Enter to continue ... ";  cin.ignore(80, '\n');


112:     const char* dataType11 = typeid(list<TrialType>::value_type).name();
113:     cout << "\nlist<TrialType>::value_type\nis equivalent to the "
114:         "following type: " << dataType11 << endl;
115:     cout << "Press Enter to continue ... ";  cin.ignore(80, '\n');


118:     const char* dataType12 = 
119:         typeid(list<TrialType>::allocator_type).name();
120:     cout << "\nlist<TrialType>::allocator_type\nis equivalent "
121:         "to the following type: " << dataType12 << endl;
122:     cout << "Press Enter to continue ... ";  cin.ignore(80, '\n');
123: }