简单记录下我的学习过程 (代码为主)
//大部分容器适用、不适用于list容器
sort(b,e)
sort(b,e,p)
stable_sort(b,e)
stable_sort(b,e,p)
/**------http://blog.csdn.net/u010579068------**/ #include<iostream> #include<cstdio> #include<string> #include<vector> #include<list> #include<deque> #include<algorithm> using namespace std; /***************************************** //大部分容器适用、不适用于list容器 sort(b,e) sort(b,e,p) stable_sort(b,e) stable_sort(b,e,p) *****************************************/ /**---------------------------------------------------------------------------------- 注意:不适用于list容器,list有成员函数sort() ----------------------------------------------------------------------------------**/ /************************************************************************************* std::sort 所有排序容器适用 algorithm -------------------------------------------------------------------------------------- template <class RandomAccessIterator> void sort ( RandomAccessIterator first, RandomAccessIterator last ); template <class RandomAccessIterator, class Compare> void sort ( RandomAccessIterator first, RandomAccessIterator last, Compare comp ); //eg: *************************************************************************************/ /************************************************************************************* std::stable_sort 所有排序容器适用 algorithm -------------------------------------------------------------------------------------- template <class RandomAccessIterator> void stable_sort ( RandomAccessIterator first, RandomAccessIterator last ); template <class RandomAccessIterator, class Compare> void stable_sort ( RandomAccessIterator first, RandomAccessIterator last, Compare comp ); //eg: *************************************************************************************/ bool myfunction (int i,int j) { return (i<j); } struct myclass { bool operator() (int i,int j) { return (i<j); } } myobject; bool compare_as_ints (double i,double j) { return (int(i)<int(j)); } int main () { int myints[] = {32,71,12,45,26,80,53,33}; vector<int> myvector (myints, myints+8); // 32 71 12 45 26 80 53 33 vector<int>::iterator it; // using default comparison (operator <): sort (myvector.begin(), myvector.begin()+4); //(12 32 45 71)26 80 53 33 // using function as comp sort (myvector.begin()+4, myvector.end(), myfunction); // 12 32 45 71(26 33 53 80) // using object as comp sort (myvector.begin(), myvector.end(), myobject); //(12 26 32 33 45 53 71 80) // print out content: cout << "myvector contains:"; for (it=myvector.begin(); it!=myvector.end(); ++it) cout << " " << *it; cout << endl; /**---------------------------------------------------------------------------------------**/ double mydoubles[] = {3.14, 1.41, 2.72, 4.67, 1.73, 1.32, 1.62, 2.58}; deque<double> mydeque; deque<double>::iterator id; mydeque.assign(mydoubles,mydoubles+8); cout << "using default comparison:"; stable_sort (mydeque.begin(), mydeque.end()); for (id=mydeque.begin(); id!=mydeque.end(); ++id) cout << " " << *id; mydeque.assign(mydoubles,mydoubles+8); cout << "\nusing 'compare_as_ints' :"; stable_sort (mydeque.begin(), mydeque.end(), compare_as_ints); for (id=mydeque.begin(); id!=mydeque.end(); ++id) cout << " " << *id; cout << endl; return 0; }
myvector contains: 12 26 32 33 45 53 71 80 using default comparison: 1.32 1.41 1.62 1.73 2.58 2.72 3.14 4.67 using 'compare_as_ints' : 1.41 1.73 1.32 1.62 2.72 2.58 3.14 4.67