如何查看地图中是否存在密钥?

Pra*_*van 2 c++ stl map

我有两个stl地图map<int,int> ,我想比较它们..所以这里是代码..

map <int, int> a,b;
insert into a and b;
map<int,int>::iterator i;

for(i=a.begin();i!=a.end();i++){
    if(what should be here?)
         then cout << (*i).first << " also present in b" << endl;
}
Run Code Online (Sandbox Code Playgroud)

我希望像(b [(*i).first])之类的东西存在?

Naw*_*waz 7

使用map::find如:

for(i=a.begin(); i!=a.end(); i++)
{
  if( b.find(i->first) != b.end() )
       std::cout << (*i).first << " also present in b" << std::endl;
}
Run Code Online (Sandbox Code Playgroud)

注意i->first并且(*i).first是相同的.