Aracılığıyla paylaş


Visual C++'da map::end, map::find, map::insert, map::iterator ve map::value_type STL işlevlerini kullanma

Bu makalede, Visual C++'da , , , ve map::value_type Standart Şablon Kitaplığı (STL) simgelerinin map::endnasıl kullanılacağı gösterilmektedirmap::iterator. map::insertmap::find

Özgün ürün sürümü: Visual C++
Özgün KB numarası: 157159

Gerekli başlık

<map>

Prototip

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

Not

Prototiplerdeki sınıf/parametre adları üst bilgi dosyasındaki sürümle eşleşmeyebilir. Bazıları okunabilirliği geliştirmek için değiştirildi.

Açıklama

işlevi, end() bir sıranın sonunu bir kez işaret eden bir yineleyici döndürür.

Bul, sıralama anahtarı değerine eşit keyolan ilk öğeyi seçen bir yineleyici döndürür. Böyle bir öğe yoksa yineleyici eşittir end().

Anahtar henüz yoksa, insert diziye ekler ve döndürür pair<iterator, true>. Anahtar zaten varsa, insert diziye eklemez ve döndürür pair <iterator, false>.

Aşağıdaki örnek, dizelerle bir int eşlemesi oluşturur. Bu durumda, eşleme basamaklardan dize eşdeğerlerine (1 -> Bir, 2 -> İki vb.) yapılır.

Program kullanıcıdan bir sayı okur, her basamak için eşdeğer sözcüğü bulur (haritayı kullanarak) ve sayıyı bir dizi sözcük olarak geri yazdırır. Örneğin, kullanıcı 25463 girerse, program şununla yanıt verir: İki Beş Dört Altı Üç.

Örnek kod

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

Program çıkışı:

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

Başvurular

, map::findve map::inserthakkında map::endaynı bilgiler için map::insert, map::find ve map::end adresini ziyaret edin.