771 B
771 B
排序
升序排序:
void ascending(std::vector<int> &x) {
// #include <algorithm>
std::sort(x.begin(), x.end());
}
降序排序:
void descending(std::vector<int> &x) {
// #include <algorithm>
// #include <functional>
std::sort(x.begin(), x.end(), std::greater<int>());
}
自定义比较函数:
struct Data {
std::string name;
int age;
};
/**
* @brief 自定义比较函数
*
* @param a 第一个待比较的值
* @param b 第二个待比较的值
* @return 返回为 true 时代表 a 应该放在 b 前面
*/
bool compareFunc(Data &a, Data &b) {
return a.name.length() > b.name.length();
}
void customCompare(std::vector<Data> &x) {
std::sort(x.begin(), x.end(), compareFunc);
}