//sortVector.cpp --- demo using sort in to sort a vector // @CopyLeft by tsaiwn@csie.nctu.edu.tw #include #include #include #include #include #include using namespace std; // comparison, not case sensitive. bool compare_nocase (string first, string second) { unsigned int i=0; while ( (itolower(second[i])) return false; ++i; } if (first.length() x) { for(int i=0; i< x.size( ); ++i) cout << " " << x[i]; cout << endl; } /////////////////// int main ( ) { vector xx; xx.push_back("aaa"); xx.push_back("ccc"); xx.push_back("bbb"); xx.push_back("BBS"); xx.push_back("egg"); xx.push_back("EGG_BIG"); cout << "vector xx: "; printVector(xx); sort(xx.begin( ), xx.end( ) ); // ascending cout << "\n=== after sort\n vector xx: "; printVector(xx); cout << "\n=== now calling sort with mycomp.. "; sort(xx.begin( ), xx.end( ) , mycomp); cout << "\n=== after sort(..., mycomp)\n vector xx: "; printVector(xx); sort(xx.begin( ), xx.end( ) , compare_nocase); cout << "\n=== after sort(..., compare_nocase)\n vector xx: "; printVector(xx); return 0; } /****** C:\testc>g++ sortVector.cpp C:\testc>a.exe vector xx: aaa ccc bbb BBS egg EGG_BIG === after sort vector xx: BBS EGG_BIG aaa bbb ccc egg === now calling sort with mycomp.. haha haha haha haha haha === after sort(..., mycomp) vector xx: egg ccc bbb aaa EGG_BIG BBS === after sort(..., compare_nocase) vector xx: aaa bbb BBS ccc egg EGG_BIG C:\testc> *******************************/