C++ STL之map使用小技巧——判断Key值是否存在

判断Key值是否存在 C++

  1. 通过map的count接口函数来判断
if(myMap.conunt(iKey) == 1)
{
	//说明IKey存在于myMap;
}
else
{
	//myMap.conunt(iKey) == 0;
	//说明IKey不存在于myMap;
}
  1. 通过map的find接口函数来判断
if(myMap.find(iKey) != myMap.end())
{
	//说明IKey存在于myMap;
}
else
{
	//myMap.find(iKey) == myMap.end();
	//说明IKey不存在于myMap;
}

我个人觉得这个是两种比较好的方法,更推荐第一种。

发布了84 篇原创文章 · 获赞 63 · 访问量 9万+

猜你喜欢

转载自blog.csdn.net/wsq119/article/details/103871457