Source of deque_b.cpp


  1: //deque_b.cpp

  3: #include <iostream>
  4: #include <deque>
  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 each typedef defined "
 12:         "by a deque 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 deque.";
 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:     deque<TrialType> q(a, a+5);
 30:     cout << "\nSize of q = " << q.size();
 31:     cout << "\nContents of q: ";
 32:     for(deque<int>::size_type i=0; i<q.size(); i++)
 33:         cout << q.at(i) << " ";
 34:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

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


 47:     deque<TrialType>::difference_type d;
 48:     d = q.end()-q.begin(); //Typical assignment to d
 49:     const char* dataType2 = typeid(d).name();
 50:     cout << "\ndeque<TrialType>::difference_type\nis equivalent "
 51:         "to the following type:\n\n" << dataType2
 52:         << "\n\nIf d is of type deque<TrialType>::differencee_type "
 53:         "and\nd = q.end()-q.begin();\nthen the value of d is "
 54:         << static_cast<int>(d) << ", for the given q.\n";
 55:     cout << "Press Enter to continue ... ";  cin.ignore(80, '\n');


 58:     deque<TrialType>::reference r = q[3];
 59:     //Typical initialization of r
 60:     //Note that references must be initialized, not assigned to.
 61:     const char* dataType3 = typeid(r).name();
 62:     cout << "\ndeque<TrialType>::reference\nis equivalent "
 63:         "to the following type:\n\n" << dataType3
 64:         << "\n\nIf r is of type deque<TrialType>::reference and\n"
 65:         "r = q[3];\nthen the value of r is "
 66:         << r << ", for the given q.\n";
 67:     cout << "Press Enter to continue ... ";  cin.ignore(80, '\n');

 69:     deque<TrialType>::const_reference r_c = q[3];
 70:     const char* dataType4 = typeid(r_c).name();
 71:     cout << "\ndeque<TrialType>::const_reference\nis equivalent "
 72:         "to the following type:\n\n" << dataType4
 73:         << "\n\nIf r_c is of type deque<TrialType>::const_reference "
 74:         "and\nr_c = q[3];\nthen the value of r is "
 75:         << r << ", for the given q.\n";
 76:     cout << "Press Enter to continue ... ";  cin.ignore(80, '\n');



 80:     deque<TrialType>::iterator itr;
 81:     itr =  q.begin()+2; //Typical assignment to itr
 82:     const char* dataType5 = typeid(itr).name();
 83:     cout << "\ndeque<TrialType>::iterator\nis equivalent to the "
 84:         "following type:\n\n" << dataType5
 85:         << "\n\nIf itr is of type deque<TrialType>::iterator "
 86:         "and\nitr = begin()+2;\nthen the value pointed to by itr is "
 87:         << *itr << ", for the given q.\n";
 88:     cout << "Press Enter to continue ... ";  cin.ignore(80, '\n');

 90:     deque<TrialType>::const_iterator itr_c;
 91:     itr_c = q.begin()+1; //Typical assignment to itr_c
 92:     const char* dataType6 = typeid(itr_c).name();
 93:     cout << "\ndeque<TrialType>::const_iterator\nis equivalent "
 94:         "to the following type:\n\n" << dataType6
 95:         << "\n\nIf itr_c is of type deque<TrialType>::const_iterator "
 96:         "and\nitr_c = begin()+1;\nthen the value pointed to by itr_c is "
 97:         << *itr_c << ", for the given q.\n";
 98:     cout << "Press Enter to continue ... ";  cin.ignore(80, '\n');



102:     deque<TrialType>::reverse_iterator itr_r;
103:     itr_r = q.rbegin()+1; //Typical assignment to itr_r
104:     const char* dataType7 = typeid(itr_r).name();
105:     cout << "\ndeque<TrialType>::reverse_iterator\nis equivalent "
106:         "to the following type:\n\n" << dataType7
107:         << "\n\nIf itr_r is of type deque<TrialType>::reverse_iterator "
108:         "and\nitr_r = rbegin()+1;\nthen the value pointed to by itr_r is "
109:         << *itr_r << ", for the given q.\n";
110:     cout << "Press Enter to continue ... ";  cin.ignore(80, '\n');

112:     deque<TrialType>::const_reverse_iterator itr_cr;
113:     itr_cr = q.rend()-2; //Typical assignment to itr_cr
114:     const char* dataType8 = typeid(itr_cr).name();
115:     cout << "\ndeque<TrialType>::const_reverse_iterator\nis equivalent "
116:         "to the following type:\n\n" << dataType8
117:         << "\n\nIf itr_cr is of type "
118:         "deque<TrialType>::const_reverse_iterator and"
119:         "\nitr_cr = rend()-2;\nthen the value pointed to by itr_cr is "
120:         << *itr_cr << ", for the given q.\n";
121:     cout << "Press Enter to continue ... ";  cin.ignore(80, '\n');



125:     deque<TrialType>::pointer ptr;
126:     ptr = &q[3]; //A typical assignment to ptr
127:     const char* dataType9 = typeid(ptr).name();
128:     cout << "\ndeque<TrialType>::pointer\nis equivalent "
129:         "to the following type:\n\n" << dataType9
130:         << "\n\nIf ptr is of type deque<TrialType>::pointer and\n"
131:         "ptr = &q[3];\nthen the value pointed to by ptr is "
132:         << *ptr << ", for the given q.\n";
133:     cout << "Press Enter to continue ... ";  cin.ignore(80, '\n');

135:     deque<TrialType>::const_pointer ptr_c;
136:     ptr_c = &q[1]; //Typical assignment to ptr_c
137:     const char* dataType10 = typeid(ptr_c).name();
138:     cout << "\ndeque<TrialType>::const_pointer\nis equivalent "
139:         "to the following type:\n\n" << dataType10
140:         << "\n\nIf ptr_c is of type deque<TrialType>::const_pointer "
141:         "and\nptr_c = &q[1];\nthen the value pointed to by ptr_c is "
142:         << *ptr_c << ", for the given q.\n";
143:     cout << "Press Enter to continue ... ";  cin.ignore(80, '\n');


146:     const char* dataType11 = typeid(deque<TrialType>::value_type).name();
147:     cout << "\ndeque<TrialType>::value_type\nis equivalent to the "
148:         "following type:\n\n" << dataType11 << endl << endl;
149:     cout << "Press Enter to continue ... ";  cin.ignore(80, '\n');


152:     const char* dataType12 = 
153:         typeid(deque<TrialType>::allocator_type).name();
154:     cout << "\ndeque<TrialType>::allocator_type\nis equivalent "
155:         "to the following type:\n\n" << dataType11 << endl << endl;
156:     cout << "Press Enter to continue ... ";  cin.ignore(80, '\n');
157: }