[問題] 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