[問題] 把 deque 當成 map 的 key 時出問題...

看板C_and_CPP作者 (低史耐爾)時間13年前 (2011/06/21 21:23), 編輯推噓0(0018)
留言18則, 4人參與, 最新討論串1/1
開發平台(Platform): (Ex: VC++, GCC, Linux, ...) Lunux + GCC 額外使用到的函數庫(Library Used): (Ex: OpenGL, ...) 只有用到 STL 問題(Question): 因為某些因素,我會需要把 deque 當成 map 的 key, 不過之前沒用過這種寫法, 所以先寫一個很簡單的小程式測試一下, 結果就出了一個摸不透的問題... deque 可以順利地當成是 map 的 key insert 進 map, 不過當我要宣告一個 deque iterator 想要去 output deque 裡面的值時, 就出現一個讓我摸不著頭緒的 error: map.cpp: In function 'int main()': 宣告 deque iterator 並且 assign begin 給這個 iterator 的那行 map.cpp:51: error: conversion from 'std::_Deque_iterator< std::basic_string< char, std::char_traits<char>, std::allocator<char> >, const std::basic_string< char, std::char_traits<char>, std::allocator<char> >&, const std::basic_string< char, std::char_traits<char>, std::allocator<char> >*>' to non-scalar type 'std::_Deque_iterator< std::basic_string< char, std::char_traits<char>, std::allocator<char> >, std::basic_string< char, std::char_traits<char>, std::allocator<char> >&, std::basic_string< char, std::char_traits<char>, std::allocator<char> >* >' requested 我知道 STL 出錯的時候的 error 很難看, 所以有稍微整理一下。 不過我看這個 error, 轉換前後的 type 是一樣的啊 = =? 這讓我摸不透... 不知道有沒有前輩可以幫忙指點一下, 感謝。 餵入的資料(Input): 無輸入 預期的正確結果(Expected Output): 可以正常 compile 過 錯誤結果(Wrong Output): 上面那串又臭又長的 STL error message...... 程式碼(Code):(請善用置底文網頁, 記得排版) http://codepad.org/8kBQ3JG6 補充說明(Supplement): -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.138.152.175

06/21 21:36, , 1F
啊,自己解了,key好像是 const type,要用 const it 去接
06/21 21:36, 1F

06/21 21:37, , 2F
沒人有反應的話等等自刪......丟臉= =|||
06/21 21:37, 2F

06/21 21:39, , 3F
這個板不能自刪 XD 就留著讓大家參考吧
06/21 21:39, 3F

06/21 21:40, , 4F
deque<string> temp = map_it->first;
06/21 21:40, 4F

06/21 21:40, , 5F
deque<string>::iterator deque_it = temp.begin();
06/21 21:40, 5F

06/21 21:40, , 6F
while( deque_it != temp.end() )
06/21 21:40, 6F

06/21 21:43, , 7F
會丟臉嘛? 至少我ㄧ眼就沒看出來,而且const type不能
06/21 21:43, 7F

06/21 21:43, , 8F
nonconst type,還有不要再亂刪內容了,昨天才有人玩這招
06/21 21:43, 8F

06/21 21:46, , 9F
const type不能assign nonconst type值嗎?
06/21 21:46, 9F

06/21 21:55, , 10F
code勒???
06/21 21:55, 10F

06/21 22:00, , 11F
Code 我放在連結的網址裡面啊 0.0 不是說要用置底文的那些
06/21 22:00, 11F

06/21 22:00, , 12F
來貼 code 嗎?
06/21 22:00, 12F

06/21 22:01, , 13F
刪內容是指?
06/21 22:01, 13F

06/21 22:06, , 14F
不用懷疑,他真的把所有發問的內容刪除,只留下幾句話
06/21 22:06, 14F

06/21 22:06, , 15F
他原本應該是打算刪除發問的內容吧,只是不能自刪
06/21 22:06, 15F

06/21 22:07, , 16F
non_const_iterator = const_iterator 好像是這樣會有問題?
06/21 22:07, 16F

06/21 22:09, , 17F
阿...對厚.我想顛倒了.謝謝.那個解法是用c++的轉型
06/21 22:09, 17F

06/21 23:53, , 18F
sorry 原來有貼...
06/21 23:53, 18F
文章代碼(AID): #1E09jBF4 (C_and_CPP)