CC++ <set> 仿函数

    科技2022-08-10  101

    仿函数

    完善优化中。。。

    #include <iostream> #include <Windows.h> #include <set> #include <functional> #include <algorithm> using namespace std; class student { public: student(int age) { this->age = age; } bool operator < (const student& right)const { return this->age < right.age; } int getAge()const { return this->age; } private: int age; }; class FunStudent { public: bool operator()(const student& left, const student& right)const { cout << "FunStudent函数" << endl; return left.getAge() < right.getAge(); /*ret = left.getAge() < right.getAge(); return ret;*/ } public: }; int main(void) { set<student, FunStudent> s; student hh(18); student xx(80); FunStudent fun; int ret = fun(hh, xx); cout << "比较结果:" << ret << endl; s.insert(hh); s.insert(xx); //set<student> stu; //stu.insert(student(18)); //stu.insert(student(19)); for (set<student, FunStudent>::iterator it = s.begin(); it != s.end(); ++it) { cout << it->getAge(); cout << " "; } cout << endl; /* set<int, less<int>> A; set<int> setInt; multiset<int> msetInt; for (int i = 0; i < 10; i++) { setInt.insert(100 - i); } setInt.insert(99); for (set<int>::iterator it = setInt.begin(); it != setInt.end(); ++it) { cout << *it; cout << " "; } cout << endl; */ system("pause"); return 0; }

    完善优化中。。。

    Processed: 0.009, SQL: 8