#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
using namespace std;
bool comp(int a, int b)
{
return a < b;
}
struct customLess {
bool operator()(int a, int b) const
{
return a < b;
}
};
struct {
bool operator()(int a, int b) const
{
return a < b;
}
} customLess2;
int main()
{
// 用自定义函数对象排序
vector<int> s = { 4,2,5,1,3 };
std::sort(s.begin(), s.end(), customLess2); //通过类的对象调用函数
for (auto a : s) {
std::cout << a << " ";
}
std::cout << '\n';
std::sort(s.begin(), s.end(), customLess()); //直接通过类调用函数
for (auto a : s) {
std::cout << a << " ";
}
std::cout << '\n';
customLess aa;
std::sort(s.begin(), s.end(), aa); //通过类的对象调用函数
for (auto a : s) {
std::cout << a << " ";
}
std::cout << '\n';
std::sort(s.begin(), s.end(), comp); //直接调用函数
for (auto a : s) {
std::cout << a << " ";
}
std::cout << '\n';
return 0;
}
![]()