功能描述:
实现关系对比 #include <iostream> #include <functional> #include <vector> #include <algorithm> using namespace std; //关系仿函数 class Compare { public: bool operator()(int v1, int v2) { return v1 > v2; } }; void test() { vector<int> v; v.push_back(10); v.push_back(30); v.push_back(50); v.push_back(40); v.push_back(20); for (vector<int>::iterator it = v.begin(); it != v.end(); ++it) { cout << *it << " "; } cout << endl; //自己实现仿函数 //sort(v.begin(), v.end()); //STL内建仿函数 大于仿函数 sort(v.begin(), v.end(), greater<int>()); for (vector<int>::iterator it = v.begin(); it != v.end(); ++it) { cout << *it << " "; } cout << endl; } int main() { test(); return 0; }