[问题] map insert错误问题

楼主: waquey   2018-06-21 21:37:57
开发平台(Platform): (Ex: Win10, Linux, ...)
C/C++
编译器(Ex: GCC, clang, VC++...)+目标环境(跟开发平台不同的话需列出)
VC++
额外使用到的函数库(Library Used): (Ex: OpenGL, ...)
std::map
问题(Question):
在执行insert时,跳出以下errors
喂入的资料(Input):
typedef struct AAA
{
float score;
bool compare;
} AAA;
预期的正确结果(Expected Output):
insert成功
错误结果(Wrong Output):
Error C2678 binary '<': no operator found which takes a left-hand operand
of type 'const AAA' (or there is no acceptable
conversion)
C:\Program Files (x86)\Microsoft Visual Studio
14.0\VC\include\xstddef 239
程式码(Code):(请善用置底文网页, 记得排版,禁止使用图档)
std::map<AAA, int>AAAInfo;
AAA *pInfo = new AAA[1];
pInfo[0].score = 90;
pInfo[0].compare = true;
int nIndex = 2;
AAAInfo.insert(std::make_pair(pInfo[0], nIndex));
补充说明(Supplement):
用::value_type也一样
作者: bluesoul (忙死你老爸)   2018-06-21 21:48:00
你没有定义AAA如何比较大小
作者: sarafciel (Cattuz)   2018-06-21 21:50:00
std::map是红黑树 你要定义compare operator才能找到在树上的位置
楼主: waquey   2018-06-21 23:09:00
请问是类似 inline bool operator< (const AAA& lhs, constAAA& rhs){ return lhs < rhs; } 这样吗?
作者: sarafciel (Cattuz)   2018-06-22 00:51:00
大括号内是错的 不过我先好奇一下 你为什么会想用AAA当key? 纯粹写来研究的?XD
作者: steve1012 (steve)   2018-06-22 04:11:00
map 是sorted的 他不知道怎么比较你定义的AAA
楼主: waquey   2018-06-22 08:58:00
是写来研究没错XD 谢谢回答喔 括号里应该要指定像是lhs.score这一类的比较
作者: sarafciel (Cattuz)   2018-06-22 12:56:00
嗯嗯 那样写就对了XD

Links booklink

Contact Us: admin [ a t ] ucptt.com