C++谓词二元谓词

tech2026-06-13  2

#include <iostream> #include <vector> #include <algorithm> using namespace std; //二元谓词 class Mycompare { public: bool operator()(int num1, int num2) { return num1 > num2; } }; void test() { vector<int> v; v.push_back(10); v.push_back(40); v.push_back(20); v.push_back(30); v.push_back(50); //默认从小到大 sort(v.begin(), v.end()); for (vector<int>::iterator it = v.begin(); it != v.end(); ++it) { cout << *it << " "; } cout << endl; cout << "-----------------------------" << endl; //使用函数对象改变算法策略,排序从大到小 sort(v.begin(), v.end(), Mycompare()); for (vector<int>::iterator it = v.begin(); it != v.end(); ++it) { cout << *it << " "; } cout << endl; } int main() { test(); return 0; }
最新回复(0)