Compartir a través de


map::insert, map::find, y map::end

Muestra cómo utilizar mapa:: inserción,mapa:: búsqueda, y funciones de la biblioteca estándar de la plantilla (STL) de mapa:: final en Visual C++.

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

Comentarios

[!NOTA]

La clase y los nombres de parámetro en el prototipo no coincide con la versión del archivo de encabezado.Algunos se han modificado para mejorar la legibilidad.

La función de final devuelve un iterador esos puntos un último el final de una secuencia.búsqueda devuelve un iterador que señala el primer elemento cuya clavede equals criterio de ordenación.Si no existe ningún elemento, el iterador es igual a final.Si la clave ya no existe, inserción la agregará a la secuencia y retorno pair<iterador, TRUE>.Si la clave ya existe, inserción no lo agregue a la secuencia y no devuelve pair <iterador, Falso>.El ejemplo siguiente se crea un mapa de s para inten s de cadena.En este caso, la asignación es de dígitos a sus equivalentes de la cadena (1 - > “Uno”, 2 - > “Dos”, etc.).El programa lee un número de usuario, busque el equivalente de word para cada dígito (mediante el mapa), e imprime la reproducción del número como una serie de palabras.Por ejemplo, si el usuario especifica 25463, el programa responde con: dos cinco cuatro seis tres.

Ejemplo

// 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;
   }
}

Entrada

12
q

Resultados del ejemplo

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

Requisitos

encabezado: <Asignar>

Vea también

Conceptos

Ejemplos de biblioteca de plantillas estándar