c++ - 在保持相对顺序的同时对向量进行排序
问题描述
我一直在尝试实现一个 c++ 程序,该程序按升序对向量向量进行排序,同时在出现平局的情况下保持当前顺序(例如,如果向量 a 在向量 b 之前,则 a 在 b 之前)。这是我现在拥有的代码。任何提示将不胜感激!
编辑:顺便说一句, c 是您排序的列
sort(v.begin(), v.end(), [=] (vector<int> &a, vector<int> &b) {
if (a[c] == b[c]) {
int d1 = find(v.begin(), v.end(), a)-v.begin();
int d2 = find(v.begin(), v.end(), b)-v.begin();
return d1 < d2;
}
return a[c] < b[c];
});
解决方案
只需使用std::stable_sort
并提供标准进行排序:
std::stable_sort(v.begin(), v.end(), [=] (vector<int> &a, vector<int> &b) {
return a[c] < b[c];
});
以非降序对范围 [first, last) 中的元素进行排序。保证保持等价元素的顺序。
重点是我的
推荐阅读
- r - R中的中位数乘以组
- r - 如何使用 R 从文本文件中提取包含特定单词或字符的句子
- c# - YamDocument 到文本表示以 3 个点结尾
- node.js - 后端设计——处理单个资源和多个资源,还是只处理多个?
- python - 卸载在 C++ 中使用 dlopen() 打开的共享对象(.so 文件)
- javascript - Edge 和 IE 中的 JsRender/JsViews 性能问题
- rest - 来自 Insomnia 和 Postman 对 WebRequest 的不同响应
- c# - ASP.NET Core 2.0+ - 如何使声明保持最新
- javafx - JavaFX 应用程序名称未显示在任务管理器或 Dock 中
- c# - 作为来自 Localhost 的虚拟应用服务标识向 KeyVault 进行身份验证