Condividi tramite


map::insert, map::find, e map::end

Viene illustrato come utilizzare mapping:: inserire,mapping:: ricerca, e mapping:: estremità Funzioni della libreria di modelli standard (STL) in Visual C++.

iterator map::end( ); 
iterator map::find(
   const Key& Key
);
pair<iterator, bool> 
map::insert(
   const value_type& x
);

Note

[!NOTA]

La classe/nomi di parametro nel prototipo non corrisponde alla versione nel file di intestazione.alcuni sono stati modificati per migliorare la leggibilità.

estremità la funzione restituisce un iteratore che punta un'esperienza la fine di una sequenza.ricerca restituisce un iteratore che definisce il primo elemento per cui la chiave di ordinamento corrisponde chiave.Se tale elemento non esiste, equals iteratori estremità.Se la chiave non esiste, inserire la aggiunto alla sequenza e al ritorno pair<iteratore, true>.se la chiave già esiste, inserire non lo aggiunge alla sequenza e restituisce pair <iteratore, false>.Nell'esempio seguente viene creato un mapping di intoggetti a stringas.In questo caso, il mapping è di cifre agli equivalenti di stringa (1 - > “uno„, 2 - > “due„, e così via).Il programma legge un numero dall'utente, cerca l'equivalente delle parole per ogni cifra (tramite il mapping di) e stampa il numero nuovamente come serie di parole.Ad esempio, se l'utente immette 25463, il programma risponde a: due cinque quattro sei tre.

Esempio

// map_insert_find_end.cpp
// compile with: /EHsc
#pragma warning(disable:4786)
#include <iostream>
#include <string>
#include <map>

using namespace std;

typedef map<int, string, less<int> > INT2STRING;

int main()
{
   // 1. Create a map of ints to strings
   INT2STRING theMap;
   INT2STRING::iterator theIterator;
   string theString = "";
   unsigned int index;

   // Fill it with the digits 0 - 9, each mapped to its string counterpart
   // Note: value_type is a pair for maps...
   theMap.insert(INT2STRING::value_type(0,"Zero"));
   theMap.insert(INT2STRING::value_type(1,"One"));
   theMap.insert(INT2STRING::value_type(2,"Two"));
   theMap.insert(INT2STRING::value_type(3,"Three"));
   theMap.insert(INT2STRING::value_type(4,"Four"));
   theMap.insert(INT2STRING::value_type(5,"Five"));
   theMap.insert(INT2STRING::value_type(6,"Six"));
   theMap.insert(INT2STRING::value_type(7,"Seven"));
   theMap.insert(INT2STRING::value_type(8,"Eight"));
   theMap.insert(INT2STRING::value_type(9,"Nine"));

   // Read a Number from the user and print it back as words
   for( ; ; )
   {
      cout << "Enter \"q\" to quit, or enter a Number: ";
      cin >> theString;
      if (theString == "q")
         break;

      // extract each digit from the string, find its corresponding
      // entry in the map (the word equivalent) and print it
      for (index = 0; index < theString.length(); index++)
      {
         theIterator = theMap.find(theString[index] - '0');
         if (theIterator != theMap.end() )   // is 0 - 9
            cout << (*theIterator).second << " ";
         else    // some character other than 0 - 9
            cout << "[err] ";
      }
      cout << endl;
   }
}

Input

12
q

Esempio di output

Enter "q" to quit, or enter a Number: 12
One Two
Enter "q" to quit, or enter a Number: q

Requisiti

intestazione: <mapping>

Vedere anche

Concetti

Esempi di una libreria di modelli standard