2010-04-05 49 views
2

我需要索引特定的字符串與其他字符串,我真的找不到一個好的方法來這樣做。我試圖使用tr1 :: unordered_map,但我在使用它時遇到一些困難。 如果有人能告訴我什麼是最好的辦法,我會非常感謝:) 我還需要通過一個數字索引對象(數字不是爲了所以我不能使用一個向量)由其他字符串索引字符串

+0

某種形式的地圖是正確的做法 - 你有什麼困難?如果您需要以兩種方式進行索引,您可能需要兩個容器。 – 2010-04-05 11:27:08

+1

@標記帽子與它有什麼關係? – bmargulies 2010-04-05 11:31:32

回答

7

std::map怎麼辦?

std::map<std::string, std::string> foo; 

然後你可以添加元素,

foo["bar"] = "baz"; 
cout << foo["bar"] << std::endl; // baz 
+0

我試圖使用這個: std :: map lang; std :: string fMatch(matches [2] .first,matches [2] .second); std :: string sMatch(matches [6] .first,matches [6] .second); lang [fMatch] = sMatch; 但我得到這個錯誤:錯誤C2679:二進制'[':找不到操作符,它需要類型'std :: string'的右側操作數(或沒有可接受的轉換) – 2010-04-05 11:39:06

+0

您可以發佈一個最小化測試用例在你的問題中,以及編譯器的輸出?該代碼看起來很合理。 – 2010-04-05 11:43:42

+0

第一個標頭:http://pastebin.com/1M6p55fG,第二個標頭:http://pastebin.com/e74mpFwj,代碼文件:http://pastebin.com/rMbyBdbU,輸出:http://pastebin.com/ wy57wMjC – 2010-04-05 12:01:11