c++ - 尝试对 std::map 进行排序时出错
问题描述
我正在尝试订购一个 std::map,这个映射有一个 int 作为键和一个类作为第二个元素。这是代码:
#include <vector>
#include <algorithm>
#include <map>
class Position {
public:
bool operator < (const Position & pos) {
return x < pos.x;
}
bool operator > (const Position & pos) {
return x > pos.x;
}
int x;
int y;
};
class Element {
public:
bool operator < (const Element & pos) {
return position.x < pos.position.x;
}
bool operator > (const Element & pos) {
return position.x > pos.position.x;
}
int order;
Position position;
};
bool IsGreater(const std::pair<int, Element>& e1,const std::pair<int, Element>& e2) {
return e1.second.position.x > e2.second.position.x;
}
using namespace std;
int main()
{
std::map<int,Element> elements;
Element e1;
Element e2;
Element e3;
e1.position.x = 2;
e2.position.x = 1;
e3.position.x = 0;
elements.insert(std::pair<int, Element>(2, e1));
elements.insert(std::pair<int, Element>(1, e2));
elements.insert(std::pair<int, Element>(0, e3));
std::sort(elements.begin(), elements.end(), IsGreater);
for (unsigned int x = 0; x < elements.size(); x++) {
printf("Element %d order: %d\n", x, elements.at(x).order);
}
return 0;
}
基本上我有 2 个类,它们是 Position(包含 x 和 ay 坐标)和一个 Element(包含索引和位置)。我需要通过位置中的 x 坐标来订购此地图,但是当我尝试编译代码时,我遇到了这 3 个问题:
Error C2676 binary '-': 'const std::_Tree_unchecked_iterator<std::_Tree_val<std::_Tree_simple_types<std::pair<const int,Element>>>>' does not define this operator or a conversion to a type acceptable to the predefined operator TestSort C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.25.28610\include\algorithm 3506
Error C2672 '_Sort_unchecked': no matching overloaded function found TestSort C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.25.28610\include\algorithm 3506
Error C2780 'void std::_Sort_unchecked(_RanIt,_RanIt,iterator_traits<_Iter>::difference_type,_Pr)': expects 4 arguments - 3 provided TestSort C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.25.28610\include\algorithm 3506
最初我试图用elements.begin()和elements.end()调用std :: sort,没有用,然后我尝试在元素类中包含运算符oveload“<”和“>”......没有运气,然后我尝试添加运算符重载“<”和“>”......但再次没有运气!我尝试使用 lambda 表达式作为 sort 方法的第三个参数,什么都没有……最后我尝试为返回 bool 的排序创建一个单独的函数……但没有任何效果。我根本无法理解问题所在,也无法找出问题所在。我的问题是:1)这个错误告诉我什么?2)我该如何解决?提前感谢您的帮助!
解决方案
你不能std::sort
在 a 上使用,std::map
因为
std::sort
需要随机访问迭代器,但std::map<Key, T>::iterator
只是双向迭代器。std::map<Key, T>::value_type
是std::pair<const Key, T>
,因此您无法更改Key
值的 ,如 中所要求的那样std::sort
。这样做的原因正是因为std::map
按设计排序,所以您不应该手动尝试对值进行“重新排序”;这样做基本上会破坏容器,导致未定义的行为。
如果您想按其他条件对 map 中的值进行排序,您应该重新评估 astd::map
是否真的是您想要的;也许 astd::vector<std::pair<Key, T>>
会更好,或者您必须复制值或将引用引用到其他容器(即 a std::vector
)并对其进行排序。
推荐阅读
- c - C语言,if语句混淆
- hybris - 在 smartedit 中打开 spartacus 在浏览器控制台中给出未捕获的错误
- android - 这些字符被添加到随机文件中。我无法理解错误的原因
- laravel - 中间件 Laravel 6 中的数据库访问
- reactjs - 在 React 上管理用户从游戏“房间”的退出?
- php - 为什么在我从 PHP 5.6 迁移到 7.3 之后,函数“session_regenerate_id”停止工作?
- javascript - 通过字符串从对象数组和子对象数组中获取动态值
- php - 如果您在另一个文件夹中有 index.php,如何在主页中设置它?
- c# - 无法从 GET 响应中获取数据
- reactjs - ./node_modules/material-ui-icons/AccessAlarm.js 找不到模块:无法解析“material-ui/SvgIcon”