/* stl排序 */ #include <iostream> #include <map> #include <vector> #include <list> #include <algorithm> using namespace std; struct STComp :public binary_function<int, int, bool> { inline bool operator()(int x, int y) { //返回true,表示不用变更位置,从大到小排序 return x > y; } }; struct STPrint :public unary_function<int, bool> { inline void operator()(int x) { cout << x << " " ; } }; void test1() { vector<int> v1 = { 1,2,3,4,5,6,7,8 }; //vector排序 sort(v1.begin(), v1.end(), STComp()); for_each(v1.begin(), v1.end(), STPrint()); } void test2() { std::list<int> alist; alist.push_back(3); alist.push_back(4); alist.push_back(2); alist.push_back(7); alist.push_back(9); alist.push_back(1); //list排序 alist.sort(STComp()); for_each(alist.begin(), alist.end(), STPrint()); } int main() { test2(); getchar(); return 0; }