  1 //tstVect.cpp -- @CopyLeft by tsaiwn@csie.nctu.edu.tw
  2 //test STL <vector>  , and as well as my "vector"
  3 #include "vector"
  4 //use my "vector" if there is one in current directory; else use STL
  5 #include<iostream>
  6 #include<algorithm>
  7 using namespace std;
  8 template<class T> void print(vector<T> &x) {
  9    typename vector<T>::iterator it;
 10    for(int i=0; i < x.size( ); ++i) cout << " "<< x[i]; cout << endl;
 11    cout<< "size, capa= "<<x.size( )<<", "<<x.capacity( )<<"; ";
 12    if(x.size()==0) { cout <<" NOTHING"<<endl; return; }
 13    cout<< "head, tail="<< *x.begin( )<<", "<< *(x.end()-1)<<"; ";
 14    cout<< "front, back="<< x.front( )<<", "<< x.back( )<<endl;
 15 }// print
 16 int main( ) {    int bb[ ] = {12, 28, 3, 4, 58, 6, 37 };
 17     vector<int> x(bb, bb+5);  // only first 5 elements
 18     vector<double> y;
 19     cout << "x: "; print(x);   cout << "y: "; print(y);
 20     //cout<< "y.front, back="<< y.front( )<<", "<< y.back( )<<endl;
 21     cout << "===now do add 999 to x; add three nums to y\n";
 22     x.push_back(999);
 23     y.push_back(3.8); y.push_back(8.5); y.push_back(4.9);
 24     cout << "x[2] = "<< x[2] << endl;   // x.front( ) = 555; //test
 25     x[2] = 333;  cout << "x[2] = " << x[2] << endl;
 26     cout << "x: "; print(x);   cout << "y: "; print(y);
 27     cout << " now do sort(x.begin( ), x.end( )); " << endl;
 28     sort(x.begin( ), x.end( ));   // use sort( ) in <algorithm>
 29     cout << "x: "; print(x);
 30     cout << "x[92]===" << x[92]<< "; do x.at(2)=222 ::: ";
 31     x.at(2) = 222; cout << x.at(2) <<" ::: "<<x[2]<< endl;
 32     x.at(92) = 222; cout << x.at(92) << endl;   // cause Exception
 33     return 0;
 34 } /********              see http://www.cplusplus.com/
 35 //////// HU Ψ ۤvg  "vector"
 36 C:\testc\STL>g++ tstVector.cpp
 37 C:\testc\STL>a
 38 x:  X  12 X  28 X  3 X  4 X  58
 39 size, capa= 5, 5; head, tail=12, 58; front, back=12, 58
 40 y:
 41 size, capa= 0, 10;  NOTHING
 42 ===now do add 999 to x; add three nums to y
 43  X x[2] = 3
 44  X  X x[2] = 333
 45 x:  X  12 X  28 X  333 X  4 X  58 X  999
 46 size, capa= 6, 10; head, tail=12, 999; front, back=12, 999
 47 y:  X  3.8 X  8.5 X  4.9
 48 size, capa= 3, 10; head, tail=3.8, 4.9; front, back=3.8, 4.9
 49  now do sort(x.begin( ), x.end( ));
 50 x:  X  4 X  12 X  28 X  58 X  333 X  999
 51 size, capa= 6, 10; head, tail=4, 999; front, back=4, 999
 52  X x[92]===1818838560; do x.at(2)=222 :::  A  A 222 ::: 222
 53  A
 54 This application has requested the Runtime to terminate it in an unusual way.
 55 Please contact the application's support team for more information.
 56 //======== /// HUOΨ C++ STL  <vector>   ////////////////
 57 C:\testc\STL>ren vector vectorME
 58 C:\testc\STL>g++ tstVector.cpp
 59 C:\testc\STL>a
 60 x:  12 28 3 4 58
 61 size, capa= 5, 5; head, tail=12, 58; front, back=12, 58
 62 y:
 63 size, capa= 0, 0;  NOTHING
 64 ===now do add 999 to x; add three nums to y
 65 x[2] = 3
 66 x[2] = 333
 67 x:  12 28 333 4 58 999
 68 size, capa= 6, 10; head, tail=12, 999; front, back=12, 999
 69 y:  3.8 8.5 4.9
 70 size, capa= 3, 4; head, tail=3.8, 4.9; front, back=3.8, 4.9
 71  now do sort(x.begin( ), x.end( ));
 72 x:  4 12 28 58 333 999
 73 size, capa= 6, 10; head, tail=4, 999; front, back=4, 999
 74 x[92]===1750359155; do x.at(2)=222 ::: 222 ::: 222
 75 
 76 This application has requested the Runtime to terminate it in an unusual way.
 77 Please contact the application's support team for more information.
 78 *********************/
