std::sort 可直接对结构体数组排序,只需传入自定义比较函数(如 lambda 或函数指针),无需重载 operator

用 std::sort 配合自定义比较函数最直接
结构体数组排序不靠重载 operator,而是传一个能告诉 <code>std::sort “谁该排在前”的函数。这个函数接收两个结构体引用,返回 true 表示第一个参数应排在第二个前面。
常见错误是写成值传递或漏掉 const 引用,导致不必要的拷贝或编译失败。
- 比较函数必须是
bool返回类型,参数为const T& - 不要在 lambda 外部捕获局部变量来改排序逻辑——每次调用都应只依赖参数本身
- 如果字段是
std::string或其他非 POD 类型,确保比较操作符可用(一般默认支持)
struct Person {
std::string name;
int age;
};
std::vector<person> people = {{"Alice", 30}, {"Bob", 25}, {"Charlie", 35}};
std::sort(people.begin(), people.end(), [](const Person& a, const Person& b) {
return a.age <h3>按字符串字段排序要注意大小写和空值</h3><p>用 <code>std::string</code> 字段(如 <code>name</code>)排序时,默认是字典序,但大写字母 ASCII 值比小写小,<code>"Zebra"</code> 会排在 <code>"apple"</code> 前面。若需忽略大小写,不能直接用 <code>std::tolower</code> 处理整个字符串——它不处理 Unicode,且对空字符行为未定义。</p><div class="aritcle_card flexRow artxards">
<div class="artcardd flexRow">
<a class="aritcle_card_img" rel="nofollow" href="/xiazai/skill4025" title="C++ 算法竞赛自动化测试数据生成与校验框架"><img
src="https://img.php.cn/upload/skill/000/000/081/178988956499722.jpg" alt="C++ 算法竞赛自动化测试数据生成与校验框架" onerror="this.onerror='';this.src='/static/lhimages/moren/morentu.png'" ></a>
<div class="aritcle_card_info flexColumn">
<a rel="nofollow" href="/xiazai/skill4025" title="C++ 算法竞赛自动化测试数据生成与校验框架" class="overflowclass">C++ 算法竞赛自动化测试数据生成与校验框架</a>
<p class="overflowclass">根据原题生成新题面、验证器及完整测试数据,自动套用 testlib 模板,用于用户要求生成测试数据时。</p>
</div>
<a rel="nofollow" href="/xiazai/skill4025" title="C++ 算法竞赛自动化测试数据生成与校验框架" class="aritcle_card_btn flexRow flexcenter"><b></b><span>下载</span>
</a>
</div>
</div><ul>
<li>简单场景可用 <code>std::lexicographical_compare</code> 配合 <code>std::tolower</code> 逐字符比较</li>
<li>若字段可能为 <code>nullptr</code>(比如 C 风格 <code>char*</code>),必须先判空,否则解引用崩溃</li>
<li>中文或 UTF-8 字符串需用 ICU 或平台 API(如 Windows 的 <code>CompareStringEx</code>),<code>std::sort</code> 默认不支持</li>
</ul><pre class="brush:php;toolbar:false;">std::sort(people.begin(), people.end(), [](const Person& a, const Person& b) {
std::string a_low = a.name;
std::string b_low = b.name;
std::transform(a_low.begin(), a_low.end(), a_low.begin(), ::tolower);
std::transform(b_low.begin(), b_low.end(), b_low.begin(), ::tolower);
return a_low <h3>原生数组用指针范围调用 <code>std::sort</code> 要小心长度</h3><p>结构体原生数组(如 <code>Person arr[100]</code>)不能直接传数组名给 <code>std::sort</code>,必须显式提供首尾指针。错把长度当迭代器、或用 <code>sizeof(arr)/sizeof(*arr)</code> 算错元素数,是高频翻车点。</p>- 推荐用
std::begin(arr)和std::end(arr),它们对原生数组安全 - 如果数组是函数参数传入(退化为指针),
sizeof就失效了,必须额外传长度 - 排序后原数组地址不变,但内容顺序已变——别假设下标还对应原始索引
Person arr[3] = {{"X", 20}, {"Y", 10}, {"Z", 30}};
std::sort(std::begin(arr), std::end(arr), [](const Person& a, const Person& b) {
return a.name <h3>多字段排序要嵌套条件判断</h3><p>先按 <code>age</code> 升序,<code>age</code> 相同时再按 <code>name</code> 字典序——这不是两次 <code>sort</code> 调用能解决的(不稳定排序会打乱前序结果),而是一次比较函数里写清优先级。</p>- 用
&&连接条件:先比主字段,相等才比次字段 - 避免写成
if (a.age != b.age) return a.age —— 逻辑正确但冗余,一行 <code>return a.age != b.age ? a.age 更紧凑 - 如果某字段是浮点数(如
double score),别用==判断相等,改用std::abs(a.score - b.score)
std::sort(people.begin(), people.end(), [](const Person& a, const Person& b) {
return a.age <p>稳定性和字段可比性是隐性门槛:如果结构体含不可比较成员(如 <code>std::vector<int></int></code> 字段),或比较逻辑涉及外部状态(如全局计数器),排序结果可能未定义或不可复现。</p>C++免费学习笔记(深入):立即使用
在学习笔记中,你将探索 C++ 的入门与实战技巧!










