作者waquey ()
看板C_and_CPP
标题[问题] map insert错误问题
时间Thu Jun 21 21:37:57 2018
开发平台(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也一样
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 118.163.84.235
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/C_and_CPP/M.1529588281.A.A24.html
※ 编辑: waquey (118.163.84.235), 06/21/2018 21:39:19
1F:→ bluesoul: 你没有定义AAA如何比较大小 06/21 21:48
2F:→ sarafciel: std::map是红黑树 你要定义compare operator才能找到在 06/21 21:50
3F:→ sarafciel: 树上的位置 06/21 21:50
4F:→ waquey: 请问是类似 inline bool operator< (const AAA& lhs, const 06/21 23:09
※ 编辑: waquey (27.147.28.235), 06/21/2018 23:09:58
5F:→ waquey: AAA& rhs){ return lhs < rhs; } 这样吗? 06/21 23:11
6F:→ sarafciel: 大括号内是错的 不过我先好奇一下 你为什麽会想用AAA当 06/22 00:51
7F:→ sarafciel: key? 纯粹写来研究的?XD 06/22 00:52
8F:推 steve1012: map 是sorted的 他不知道怎麽比较你定义的AAA 06/22 04:11
9F:→ waquey: 是写来研究没错XD 谢谢回答喔 括号里应该要指定像是lhs.sc 06/22 08:58
10F:→ waquey: ore这一类的比较 06/22 08:59
11F:→ sarafciel: 嗯嗯 那样写就对了XD 06/22 12:56