1
我正在使用multimap stl,我迭代了我的地图,我没有在地图内找到想要的对象,现在我想检查一下我的迭代器是否持有这个东西想要或没有,我有困难,因为它不是零或什么的。感谢名单!如何检查我的迭代器是否没有任何东西
我正在使用multimap stl,我迭代了我的地图,我没有在地图内找到想要的对象,现在我想检查一下我的迭代器是否持有这个东西想要或没有,我有困难,因为它不是零或什么的。感谢名单!如何检查我的迭代器是否没有任何东西
如果它找不到你想要的东西,那么它应该等于容器的end()
方法返回的迭代器。
所以:
iterator it = container.find(something);
if (it == container.end())
{
//not found
return;
}
//else found
为什么你遍历你的地图上找到的东西,你应该去喜欢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"
}
是否等于map.end()? map.end()是过去的最后一个索引,因此技术上不在枚举内 – 2010-09-04 21:07:26