电话
400 9058 355
std::sort对vector默认升序排序需传入begin()和end()迭代器,按operator
std::sort
直接调用 std::sort 对 vector 排序,必须传入迭代器范围,不是容器本身。它默认按元素类型的小于关系(operator)升序排列,适用于 int、double、string 等内置或已重载比较运算符的类型:
#include#include std::vector v = {3, 1, 4, 1, 5}; std::sort(v.begin(), v.end()); // → {1, 1, 3, 4, 5}
注意:别写成 std::sort(v) 或 std::sort(v.data(), v.size()) —— 编译失败。
结构体默认没有 operator,std::sort 不知道怎么比。常见写法有三种,按推荐顺序列出:
operator(最简洁,适合单一自然序):
struct Person {
std::string name;
int age;
bool operator<(const Person& other) const {
return age < other.age; // 按年龄升序
}
};
std::vector people = {{"Alice", 30}, {"Bob", 25}};
std::sort(people.begin(), people.end()); // ✅ 可用
std::sort(people.begin(), people.end(), [](const Person& a, const Person& b) {
return a.age != b.age ? a.age < b.age : a.name < b.name; // 先按年龄,再按姓名
});
bool cmp_by_name(const Person& a, const Person& b) {
return a.name < b.name;
}
std::sort(people.begin(), people.end(), cmp_by_name);
降序不能只改 > 就完事,得确保比较逻辑严格弱序(即不满足 a 且不满足 b 时,认为相等)。错误写法:return a > b 在浮点或自定义类型中可能违反严格弱序;正确做法是翻转原比较逻辑:
std::greater() 或 lambda [](int a, int b) { return a > b; }
return a.age > b.age(前提是 age 是整型且无 NaN 等异常值)如果需要相同元素保持原有相对顺序(比如排序前两个同龄人 A 在 B 前,排完还在前面),不能用 std::sort —— 它不保证稳定。该换 std::stable_sort:
std::stable_sort(people.begin(), people.end(), [](const Person& a, const Person& b) {
return a.age < b.age;
});
std::sort 是就地排序,不改变 vector

lambda 或比较函数里,参数务必加 const&,否则编译不过(std::sort 内部传的是 const 迭代器解引用结果):
[](Person a, Person b) { ... }(值传递,效率低且可能编译失败)[](Person& a, Person& b) { ... }(非 const 引用,无法绑定 const 临时对象)[](const Person& a, const Person& b) { ... }
真正容易被忽略的是:如果你的比较逻辑依赖某个外部状态(比如按当前用户偏好动态切换排序字段),lambda 必须显式捕获([&] 或 [=]),且确保被捕获对象生命周期长于排序调用——否则运行时崩溃。
邮箱:8955556@qq.com
Q Q:8955556
本文详解如何将Go官方present工具(用于生成HTML5...
PySNMP在不同版本中对SNMP错误状态(errorSta...
time.Sleep仅阻塞当前goroutine,其他gor...
PHPfopen()创建含特殊符号的文件名失败主因是操作系统...
WooCommerce中通过代码为分组产品动态聚合子商品的属...
io.ReadFull返回io.ErrUnexpectedE...
本文详解Yii2中控制器向视图传递ActiveRecord数...
本文详解为何通过wp_set_object_terms()为...
Pytest中使用@mock.patch类装饰器会导致补丁泄...
带缓冲的channel是并发安全的FIFO队列;make(c...