在我的函数中,我有这个参数:
map<string,int> *&itemList
Run Code Online (Sandbox Code Playgroud)
我想先检查是否存在密钥.如果此键存在,则获取该值.我想这个:
map<string,int>::const_iterator it = itemList->find(buf.c_str());
if(it!=itemList->end())
//how can I get the value corresponding to the key?
Run Code Online (Sandbox Code Playgroud)
检查密钥是否存在的正确方法是什么?
是的,这是正确的方法.与键关联的值存储在迭代器的second成员中std::map.
map<string,int>::const_iterator it = itemList->find(buf.c_str());
if(it!=itemList->end())
{
return it->second; // do something with value corresponding to the key
}
Run Code Online (Sandbox Code Playgroud)