//sortVec2.cpp -- by tsaiwn@csie.nctu.edu.tw
//demo to use compare function and Comparator to sort vector
#include <vector>
#include <algorithm>
#include <iostream>
#include <stdio.h>
using namespace std;
bool comppp(int a, int b) { return a > b; }  // a compare function
//The following class is a comparator
struct God {   // default public:
   bool operator( ) (int a, int b) { return a < b; }
};
template<class T> void print(vector<T> x){  // for vector of T
   typename vector<T>::iterator i;
   for(i= x.begin( ); i!= x.end( ); ++i) cout<< " " << *i;
   cout << std::endl;
}//print(
// main program starts here
int x[ ] = { 38, 49, 25, 58, 85, 66 };
int main( ) {
   God arben;
   vector<int> v(x, x+(sizeof(x)/sizeof(x[0])));
   printf("capacity=%d, size=%d\n", v.capacity(), v.size());
   v.insert(v.begin( ), 33);  // into head
   printf("capacity=%d, size=%d\n", v.capacity(), v.size());
   v.push_back(77);
   printf("capacity=%d, size=%d\n", v.capacity(), v.size());
   v.insert(v.begin( ), 99);  // head again
   printf("capacity=%d, size=%d\n", v.capacity(), v.size());
   printf("Original vector: ");
   print(v);
   sort(v.begin(), v.end() );
   printf("After sort: ");
   print(v);
   sort(v.begin( ), v.end( ), comppp);
   printf("After sort with comppp: ");
   print(v);
   v.insert(v.begin( )+1, 68);
   printf("After v.insert(v.begin( )+1,68): "); print(v);
   sort(v.begin( ), v.end( ), arben);
   printf("After sort with arben: ");
   print(v);
   printf("capacity=%d, size=%d\n", v.capacity(), v.size());
   double y[ ] = { 3.8, 4.9, 25, 5.8, 8.5, 0.66 };
   vector<double> vyy(y, y+(sizeof(y)/sizeof(y[0])));
   cout << "vyy: "; print(vyy);
   cout << "first, last: " << vyy.front( )<<", "<< vyy.back( )<<endl;
   return 0;
}//main(
/*****************
C:\testc\STL>g++ sortVec2.cpp

C:\testc\STL>a
capacity=6, size=6
capacity=12, size=7
capacity=12, size=8
capacity=12, size=9
Original vector:  99 33 38 49 25 58 85 66 77
After sort:  25 33 38 49 58 66 77 85 99
After sort with comppp:  99 85 77 66 58 49 38 33 25
After v.insert(v.begin( )+1,68):  99 68 85 77 66 58 49 38 33 25
After sort with arben:  25 33 38 49 58 66 68 77 85 99
capacity=12, size=10
vyy:  3.8 4.9 25 5.8 8.5 0.66
first, last: 3.8, 0.66
*********************************/
