Source of vector_b.cpp


  1: //vector_b.cpp

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

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


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


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

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



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

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



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

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



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

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


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


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