マルチマップstlを使用していますが、マップを反復処理しましたが、マップ内で必要なオブジェクトが見つかりませんでした。イテレータが必要なものを保持しているかどうかを確認したいのですが、問題が発生しています。 nullか何かではありません。ありがとう!
2103 次
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 に答える