看看地图中是否有一个键c ++ [英] see if there is a key in a map c++

查看:125
本文介绍了看看地图中是否有一个键c ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的函数中,我有这个参数:

in my function, i have this parameter:

map<string,int> *&itemList

我想首先检查键是否存在。如果此键存在,则获取该值。
我认为这样:

I want to first check if a key exists. If this key exists obtain the value. I thought this:

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?

是检查键是否存在的正确方法?

is the correct way to check whether the key exists?

推荐答案

是的,这是正确的方法。与键相关的值存储在第二成员 std :: map 迭代器中。

Yes, this is correct way to do this. The value associated with the key is stored in second member of std::map iterator.

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
}

这篇关于看看地图中是否有一个键c ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆