Aracılığıyla paylaş


map::insert, map::find, ve map::end

Nasıl kullanılacağı gösterilmiştir map::insert, map::find, ve map::end Visual C++ standart şablon kitaplığı (stl) işlevleri.

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

Notlar

[!NOT]

Prototip sınıfı/parametre adları üstbilgi dosyasında sürüm eşleşmiyor.Bazıları, okumayı kolaylaştırmak için değiştirildi.

Son işlevi tek bir sıra sonunu işaret eden bir yineleyici döndürür.Bul , ilk öğe olan sıralama anahtar eşittir atayan bir yineleyici döndürür anahtar.Bu tür bir öğe varsa, Yineleyici eşittir Son.Anahtar henüz yoksa, Ekle sırası ekleyin ve iade pair<Yineleyici, doğru>.Anahtarı zaten varsa, Ekle sıra eklemez ve döner pair <Yineleyici, yanlış>.Aşağıdaki örnek bir haritasını yaratır ints dizes.Bu durumda, eşleştirme dizesi eşdeğerlerine arasında rakam yapılır (1 - > "One", 2 - > "İki", vb.).Program kullanıcıdan bir numarayı okur, word her basamak için (kullanarak bağlan) eşdeğer bulur ve numara geri sözcükleri bir dizi olarak yazdırır.Kullanıcı 25463 girerse, örneğin, program verir: iki beş dört altı üç.

Örnek

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

Girdi

12
q

Örnek Çıktı

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

Gereksinimler

Başlık: <map>

Ayrıca bkz.

Kavramlar

Standart şablon kitaplığı örnekleri