开发者

How to check if my iterator stands on nothing

开发者 https://www.devze.com 2023-01-14 16:50 出处:网络
i\'m using a multimap stl, i iterate my ma开发者_运维知识库p and i did\'nt find the object i wanted inside the map, now i want to check if my iterator holds the thing i wanted or not and i\'m having d

i'm using a multimap stl, i iterate my ma开发者_运维知识库p and i did'nt find the object i wanted inside the map, now i want to check if my iterator holds the thing i wanted or not and i'm having difficulties with it because it's not null or something. thanx!


If it doesn't find the thing you want then it should equal the iterator returned by the end() method of the container.

So:

iterator it = container.find(something);
if (it == container.end())
{
  //not found
  return;
}
//else found


Why are you iterating over your map to find something, you should go like ChrisW to find a key in your map...

Mmm, are you trying to find the value in your map and not the key? Then you should do:

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"
}
0

精彩评论

暂无评论...
验证码 换一张
取 消