Domanda

Io sto usando uno STL multimap, i iterare la mia mappa e ho did'nt trovare l'oggetto che ho voluto all'interno della mappa, ora voglio verificare se la mia iteratore tiene la cosa che volevo o no e sto avendo difficoltà con esso perché non è nullo o qualcosa del genere. Grazie!

È stato utile?

Soluzione

Se non trova la cosa che si vuole allora dovrebbe essere uguale l'iteratore restituito dal metodo end() del contenitore.

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

Altri suggerimenti

Perché si effettua l'iterazione vostra mappa per trovare qualcosa, si dovrebbe andare come ChrisW di trovare una chiave nella mappa ...

Mmm, stai cercando di trovare il valore nella mappa e non la chiave? Poi si dovrebbe fare:

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"
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top