[問題] map insert錯誤問題

看板C_and_CPP (C/C++)作者時間7年前 (2018/06/21 21:38), 7年前編輯推噓1(1010)
留言11則, 4人參與, 7年前最新討論串1/1
開發平台(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://www.ptt.cc/bbs/C_and_CPP/M.1529588281.A.A24.html ※ 編輯: waquey (118.163.84.235), 06/21/2018 21:39:19

06/21 21:48, 7年前 , 1F
你沒有定義AAA如何比較大小
06/21 21:48, 1F

06/21 21:50, 7年前 , 2F
std::map是紅黑樹 你要定義compare operator才能找到在
06/21 21:50, 2F

06/21 21:50, 7年前 , 3F
樹上的位置
06/21 21:50, 3F

06/21 23:09, 7年前 , 4F
請問是類似 inline bool operator< (const AAA& lhs, const
06/21 23:09, 4F
※ 編輯: waquey (27.147.28.235), 06/21/2018 23:09:58

06/21 23:11, 7年前 , 5F
AAA& rhs){ return lhs < rhs; } 這樣嗎?
06/21 23:11, 5F

06/22 00:51, 7年前 , 6F
大括號內是錯的 不過我先好奇一下 你為什麼會想用AAA當
06/22 00:51, 6F

06/22 00:52, 7年前 , 7F
key? 純粹寫來研究的?XD
06/22 00:52, 7F

06/22 04:11, 7年前 , 8F
map 是sorted的 他不知道怎麼比較你定義的AAA
06/22 04:11, 8F

06/22 08:58, 7年前 , 9F
是寫來研究沒錯XD 謝謝回答喔 括號裡應該要指定像是lhs.sc
06/22 08:58, 9F

06/22 08:59, 7年前 , 10F
ore這一類的比較
06/22 08:59, 10F

06/22 12:56, 7年前 , 11F
嗯嗯 那樣寫就對了XD
06/22 12:56, 11F
文章代碼(AID): #1RAwevea (C_and_CPP)
文章代碼(AID): #1RAwevea (C_and_CPP)