c++代码中的运行时错误
Run Time Error in a C++ code
我在c++代码中得到运行时错误。我给我的源代码错误信息。需要帮助!提前谢谢。
源代码:#include <map>
#include <cstdio>
using namespace std;
class Pair{
public:
int x;
int y;
};
map < Pair , int > mapper;
int main(){
Pair a;
a.x = 8;
a.y = 9;
mapper[a] = 1; // Here i get Run-Time-Error
return 0;
}
错误信息:
c:program filescodeblocksmingwbin..libgccmingw324.7.1includec++bitsstl_function.h|237|note: 'const Pair' is not derived from 'const std::multimap<_Key, _Tp, _Compare, _Alloc>'|
错误在于必须定义一个方法来提供pair之间的排序。Map不知道如何比较对象
一个例子:
#include <map>
#include <cstdio>
using namespace std;
class Pair{
public:
int x;
int y;
};
bool operator<(const Pair& l, const Pair& r) {
return l.x < r.x;
}
map < Pair , int > mapper;
int main(){
Pair a;
a.x = 8;
a.y = 9;
mapper[a] = 1;
return 0;
}
在本例中,使用x的值比较pair,但是您可以根据需要提供一个函数。
相关文章:
- 为什么此代码存在运行时错误?
- 蝴蝶代码 88.合并排序数组 c++ 运行时错误
- 在Hackerearth上获取运行时错误(SIGSEGV),无法找出代码中的问题
- 为什么我在此代码中收到运行时错误
- 我的代码上的运行时错误 sigsegv
- 矢量的 c++ 代码中的运行时错误
- C++代码在 for 循环的条件下给出运行时错误,而如果它被具有相同意义的代码替换,则编译正确
- 我的代码中出现运行时错误的可能原因是什么?
- 我在代码中收到一个运行时错误,该错误如何通过修改最多一个元素来查找数组是否可以变得不减少
- 在计算机级别,应用程序以二进制方式运行,然后当发生运行时错误时,它如何呈现回错误代码
- .NET CORE 2 DLL引用带有C 代码运行时错误
- 为什么我的代码给出运行时错误超时
- 为什么我的程序在删除以下代码中的注释后会出现运行时错误?
- 薄板样条形状转换运行时错误 [使用代码 -1073741819 退出]
- C 代码的运行时错误(基于指针)
- 代码中的运行时错误,用于求解最小的偶数可整除
- 为什么我的代码导致运行时错误
- 为什么矢量上的这段代码会给出运行时错误
- 涉及向量的初学者代码,SEIGSEV运行时错误
- 为什么这段代码会产生运行时错误