次の方法で共有


Visual C++ で map::end、map::find、map::insert、map::iterator、map::value_type STL 関数を使用する

この記事では、Visual C++ で map::endmap::findmap::insertmap::iteratormap::value_type Standard Template Library (STL) シンボルを使用する方法について説明します。

元の製品バージョン: Visual C++
元の KB 番号: 157159

必須ヘッダー

<map>

プロトタイプ

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

プロトタイプのクラス名またはパラメーター名が、ヘッダー ファイル内のバージョンと一致しない可能性があります。 読みやすさを向上させるために変更されたものもあります。

説明

end()関数は、シーケンスの末尾の 1 つ後を指す反復子を返します。

Find は、並べ替えキーが key等しい最初の要素を選択する反復子を返します。 このような要素が存在しない場合、反復子は end()と等しくなります。

キーがまだ存在しない場合は、 insert によってシーケンスに追加され、 pair<iterator, true>が返されます。 キーが既に存在する場合、 insert はシーケンスに追加せず、 pair <iterator, false>を返します。

次の例では、int のマップを文字列に作成します。 この場合、マッピングは数字から文字列に相当します (1 -> One、2 -> Two など)。

プログラムは、ユーザーから数値を読み取り、(マップを使用して) 各数字に相当する単語を検索し、一連の単語として数値を出力します。 たとえば、ユーザーが 25463 を入力した場合、プログラムは次のように応答します: 5 つの 4 つの 3 つ

サンプル コード

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

プログラムの出力:

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

関連情報

map::endmap::findmap::insertと同じ情報については、map::insert、map::find、map::end を参照してください。