Verwenden der Funktionen map::end, map::find, map::insert, map::iterator und map::value_type STL in Visual C++

In diesem Artikel wird veranschaulicht, wie die map::endSymbole , map::find, , map::insert``map::iteratorund map::value_type Standard Template Library (STL) in Visual C++ verwendet werden.

Originalversion des Produkts:   Visual C++
Ursprüngliche KB-Nummer:   157159

Erforderlicher Header

<map>

Prototypen

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);

Hinweis

Die Klassen-/Parameternamen in den Prototypen stimmen möglicherweise nicht mit der Version in der Headerdatei überein. Einige wurden geändert, um die Lesbarkeit zu verbessern.

Beschreibung

Die end() Funktion gibt einen Iterator zurück, der einen Punkt über das Ende einer Sequenz zeigt.

Find gibt einen Iterator zurück, mit dem das erste Element ausgewählt wird, dessen Sortierschlüssel gleich ist key. Wenn kein solches end()Element vorhanden ist, ist der Iterator gleich .

Wenn der Schlüssel noch nicht vorhanden ist, insert wird er der Sequenz hinzugefügt und zurückgegeben pair<iterator, true>. Wenn der Schlüssel bereits vorhanden ist, insert wird er der Sequenz nicht hinzugefügt und gibt zurück pair <iterator, false>.

Im folgenden Beispiel wird eine Zuordnung von Ints zu Zeichenfolgen erstellt. In diesem Fall erfolgt die Zuordnung von Ziffern zu ihren Zeichenfolgenentsprechungen (1 -> One, 2 -> Two usw.).

Das Programm liest eine Zahl vom Benutzer, sucht das Wortäquivalent für jede Ziffer (mithilfe der Karte) und druckt die Zahl als Eine Reihe von Wörtern zurück. Wenn der Benutzer z. B. "25463" eingibt, antwortet das Programm mit: Zwei fünf vier sechs Drei.

Beispielcode

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

Programmausgabe:

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

References

Die gleichen Informationen zu map::end, map::findund map::insert, besuchen Sie map::insert, map::find und map::end.