1

我正在使用 multimap stl,我迭代我的地图,但我没有在地图中找到我想要的对象,现在我想检查我的迭代器是否包含我想要的东西,我遇到了困难,因为它不是 null 什么的。谢谢!

4

2 回答 2

8

如果它没有找到你想要的东西,那么它应该等于end()容器方法返回的迭代器。

所以:

iterator it = container.find(something);
if (it == container.end())
{
  //not found
  return;
}
//else found
于 2010-09-04T21:07:40.267 回答
0

你为什么要遍历你的地图来寻找东西,你应该像 ChrisW 一样在你的地图中找到一个键......

嗯,您是否试图在地图中找到值而不是键?那么你应该这样做:

map<int, string> myMap;
myMap[1] = "one"; myMap[2] = "two"; // etc.

// Now let's search for the "two" value
map<int, string>::iterator it;
for( it = myMap.begin(); it != myMap.end(); ++ it ) {
   if ( it->second == "two" ) {
      // we found it, it's over!!! (you could also deal with the founded value here)
      break; 
   }
}
// now we test if we found it
if ( it != myMap.end() ) {
   // you also could put some code to deal with the value you founded here,
   // the value is in "it->second" and the key is in "it->first"
}
于 2010-09-05T03:03:44.087 回答