2014-05-07 15 views
0

這可能很簡單,但我已經清醒了太久。如何在int的地圖上使用find,字符串

所以我有一個地圖<整型,字符串>和我想要使用的功能找到

iter=mapInKey.find(int)->second; 
if (iter != mapInKey.end()) 
{ 
    outFileTxt << iter; 
} 

但我得到這個錯誤:no matching function for call to std::map<int, std::basic_string<char> >::find(std::string&)

所有我想要做的就是輸出的INT

+0

find的參數不能是int,你不能使用second。 –

回答

2

另一對值喜歡這張

std::map <int,std::string>::iterator it = mapInKey.find(val); //val is the integer key you search for 

    if(it != mapInKey.end()){ 
    // do something 
    cout<< it->second; // this will print the string 
    } 

編輯

我想你想按字符串搜索。那麼你宣佈地圖是錯誤的。使用這個

map<string,int> m; 
m["Hi"]= 1; 
m["Hello"]= 2; 
map<string,int>::iterator it = m.find("Hello"); 
if(it != m.end()) 
{ 
    cout<< it->first<<":"<<it->second<<endl; 
} 
+0

在C++ 11中,你可以編寫'auto it = .....' –

+0

我仍然收到錯誤。是我的地圖聲明錯誤的std :: map mapInKey; – user3396692

+0

@ user3396692可以發佈完整的代碼嗎? – Rakib

相關問題