Nota
L'accesso a questa pagina richiede l'autorizzazione. È possibile provare ad accedere o modificare le directory.
L'accesso a questa pagina richiede l'autorizzazione. È possibile provare a modificare le directory.
Questo articolo illustra come usare i map::endsimboli STL (Standard Template Library) , map::iteratormap::findmap::insert, , e map::value_type in Visual C++.
Versione originale del prodotto: Visual C++
Numero KB originale: 157159
Intestazione obbligatoria
<map>
Prototipi
iterator map::end();
// Key is the data type of template argument #1 for map
iterator map::find(const Key& key);
pair<iterator, bool> map::insert(const value_type& x);
Note
I nomi di classe/parametro nei prototipi potrebbero non corrispondere alla versione nel file di intestazione. Alcuni sono stati modificati per migliorare la leggibilità.
Descrizione
La end() funzione restituisce un iteratore che punta uno oltre la fine di una sequenza.
Find restituisce un iteratore che sceglie il primo elemento la cui chiave di ordinamento è uguale a key. Se non esiste alcun elemento di questo tipo, l'iteratore è end()uguale a .
Se la chiave non esiste già, insert la aggiungerà alla sequenza e restituirà pair<iterator, true>. Se la chiave esiste già, insert non la aggiunge alla sequenza e restituisce pair <iterator, false>.
Nell'esempio seguente viene creata una mappa di int a stringhe. In questo caso, il mapping proviene da cifre ai relativi equivalenti stringa (1 -> Uno, 2 -> Due e così via).
Il programma legge un numero dall'utente, trova la parola equivalente per ogni cifra (usando la mappa) e stampa il numero come una serie di parole. Ad esempio, se l'utente immette 25463, il programma risponde con: Two Five Four Six Three.
Codice di esempio
//////////////////////////////////////////////////////////////////////
// Compile options needed: None
// <filename> : main.cpp
// Functions:
// end
// find
// insert
// of Microsoft Product Support Services,
// Copyright (c) 1996 Microsoft Corporation. All rights reserved.
//////////////////////////////////////////////////////////////////////
// disable warning C4018: '<' : signed/unsigned mismatch
// okay to ignore
#pragma warning(disable: 4018)
#pragma warning(disable:4786)
#include <iostream>
#include <string>
#include <map>
using namespace std;
typedef map<int, string, less<int>, allocator<string> > INT2STRING;
void main()
{
// 1. Create a map of ints to strings
INT2STRING theMap;
INT2STRING::iterator theIterator;
string theString = "";
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;
}
}
Output del programma:
Enter "q" to quit, or enter a Number: 22
Two Two
Enter "q" to quit, or enter a Number: 33
Three Three
Enter "q" to quit, or enter a Number: 456
Four Five Six
Enter "q" to quit, or enter a Number: q
Riferimenti
Per le stesse informazioni su map::end, map::finde map::insert, visitare map::insert, map::find e map::end.