c++ sort函数的参数有哪几种
C++的sort函数有两种情势,一种是使用默许的比较函数进行排序,另外一种是使用自定义的比较函数进行排序。
使用默许比较函数进行排序时,sort函数的参数以下:
使用自定义比较函数进行排序时,sort函数的参数以下:
例如,使用默许比较函数进行排序的代码示例:
#include
#include
int main() {
std::vector nums = {5, 2, 8, 1, 4};
std::sort(nums.begin(), nums.end());
return 0;
}
使用自定义比较函数进行排序的代码示例:
#include
#include
bool compare(int a, int b) {
return a > b; // 降序排序
}
int main() {
std::vector nums = {5, 2, 8, 1, 4};
std::sort(nums.begin(), nums.end(), compare);
return 0;
}
TOP