map クラス

データ値と並べ替えキーのペアになっている要素が含まれているコレクションに対して、データの格納と取得を実行するために使用します。 キーの値は一意で、データを自動的に並べ替えるために使用されます。

map 内の要素の値は直接変更できます。 キー値は定数であり、変更できません。 ただし、要素の値を変更した場合、変更前の要素に関連付けられていたキー値を削除し、変更後の新しい要素に対して新しいキー値を挿入する必要があります。

構文

template <class Key,
    class Type,
    class Traits = less<Key>,
    class Allocator=allocator<pair <const Key, Type>>>
class map;

パラメーター

Key
map に格納されるキーのデータ型。

Type
map に格納される要素のデータ型。

Traits
2 つの要素の値を並べ替えキーとして比較して、map 内の要素の相対順序を決定できる関数オブジェクトを提供する型。 この引数は省略可能であり、既定値は二項述語 less<Key> です。

C++ 14 では、型パラメーターを使用せずに std::less<> 述語を指定することで、異種ルックアップを有効にすることができます。 詳細については、「連想コンテナーの異種ルックアップ」をご覧ください。

Allocator
メモリの map の割り当てと解放に関する詳細をカプセル化する、格納されたアロケーター オブジェクトを表す型。 この引数は省略可能であり、既定値は allocator<pair<const Key, Type> > です。

解説

C++ 標準ライブラリ map クラスには下記の特徴があります。

  • 関連付けられたキー値に基づいて要素の値を効率的に取得する可変サイズのコンテナーです。

  • 反転することができます。これは、要素にアクセスするための双方向反復子が用意されているためです。

  • 並べ替えが実行されます。これは、指定した比較関数に従いキー値によって要素に順序が設定されるためです。

  • 一意のクラスです。 これは、各要素が一意のキーを持つ必要があるためです。

  • ペアを保持する連想コンテナーです。これは、要素のデータ値とキー値が分かれているためです。

  • クラス テンプレートとして機能します。これは、このクラスに用意されている機能が汎用的な機能であり、要素やキーの型に依存しないためです。 要素やキーに使用されているデータ型は、クラス テンプレートで比較関数やアロケーターと共にパラメーターとして指定されます。

map クラスで用意されている反復子は双方向反復子ですが、insertmap の各クラス メンバー関数は、弱い入力反復子をテンプレート パラメーターとして取得するバージョンがあります。この反復子の機能に関する要件は、双方向反復子のクラスで保証されている要件よりも低くなっています。 これらの反復子の機能に差異があるのは、反復子の概念が異なっているためです。 反復子の各概念には、反復子独自の一連の要件が含まれています。また、それらの要件を使用するアルゴリズムは、反復子の要件による影響を受けます。 たとえば、一部のオブジェクトを参照するために入力反復子が逆参照される場合があり、また、シーケンス内にある次の反復子に対して逆参照が増加する場合もあります。

コンテナーの種類を選択するときは、アプリケーションで必要となる検索や挿入の種類に基づいてい選択することをお勧めします。 連想コンテナーは、検索、挿入、削除の各操作用に最適化されています。 これらの操作を明示的にサポートするメンバー関数は、最悪のシナリオ (コンテナー内にある要素の数の対数に比例して処理時間が増加する) の場合でも、これらの操作を実行します。 要素を挿入しても反復子の有効性は失われません。また、要素を削除した場合は、削除された要素を具体的に指す反復子だけが無効化されます。

値とキーを関連付ける条件をアプリケーションが満たしている場合、map が最適な連想コンテナーとして機能するように指定してください。 この種類の構造体のモデルとなるのは、一意に発生するキーワードおよび関連する文字列値 (キーワードの定義を指定) が順番に格納されたリストです。 ただし、キーワードには正しい定義が複数あり、そのためにキーが一意ではなくなる場合は、multimap が適切なコンテナーとなります。 また、キーワードのリストだけが格納される場合は、set が適切なコンテナーとなります。 キーワードを複数設定できる場合は、multiset が適切なコンテナーとなります。

map では、key_compare 型の格納されている関数オブジェクトを呼び出すことによって、map が制御する要素を並べ替えます。 この格納されているオブジェクトは比較関数であり、key_comp メソッドを呼び出すことによってアクセスできます。 通常、指定された 2 つの要素を比較することで、どちらの要素がより小さいか、またはそれらの要素は等しいかどうかを判断します。 すべての要素を比較すると、等価でない要素の順序付けられたシーケンスが作成されます。

Note

比較関数は、数学上の標準的な意味で厳密弱順序を発生させる二項述語です。 二項述語 f(x,y) は、2 つの引数オブジェクト x および y と戻り値 true または false を持つ関数オブジェクトです。 set に適用される順序付けは、二項述語が非再帰、反対称、推移的であり、等価性が推移的である (2 つのオブジェクト x と y が、f(x,y) と f(y,x) の両方が false の場合に等価になるように定義されている) 場合、厳密弱順序になります。 2 つのキーの等値に関する条件が等価性の条件よりも厳しく、優先される場合、順序付けは完全な順序付け (すべての要素が相互の値に基づいて並べ替えられる) となり、一致するそれぞれのキーを識別するのが難しくなります。

C++ 14 では、型パラメーターを使用せずに std::less<> 述語または std::greater<> 述語を指定することで、異種ルックアップを有効にすることができます。 詳細については、「連想コンテナーの異種ルックアップ」をご覧ください。

メンバー

コンストラクター

名前 説明
map 特定のサイズのリスト、特定の値の要素を持つリスト、特定の allocator を持つリストを構築します。または他の map のコピーとしてリストを構築します。

Typedefs

名前 説明
allocator_type map オブジェクトの allocator クラスの typedef。
const_iterator const 内の map 要素を読み取ることができる双方向反復子の typedef。
const_pointer map 内の const 要素へのポインターの typedef。
const_reference map に格納された const 要素への参照の typedef (読み取りと const 操作を実行するため)。
const_reverse_iterator const 内の任意の map 要素を読み取ることができる双方向反復子を提供する型。
difference_type 反復子が指す要素の範囲内にある map の要素の数に対する符号付き整数の typedef。
iterator map 内の任意の要素の読み取りまたは変更ができる双方向反復子の typedef。
key_compare 2 つの並べ替えキーを比較して、map 内の 2 つの要素の相対順序を決定できる関数オブジェクトの typedef。
key_type map の各要素に格納されている並べ替えキーの typedef。
mapped_type map の各要素に格納されているデータの typedef。
pointer map 内の const 要素へのポインターの typedef。
reference map に格納されている要素への参照の typedef。
reverse_iterator 反転された map 内の 1 つの要素の読み取りまたは変更ができる双方向反復子の typedef。
size_type map 内の要素の数に対する符号なし整数の typedef。
value_type map 内に要素として格納されるオブジェクトの種類の typedef。

メンバー関数

メンバー関数 説明
at 指定したキー値を持つ要素を検索します。
begin map 内の最初の要素を指す反復子を返します。
cbegin map 内の最初の要素を指す定数反復子を返します。
cend 定数末尾超え反復子を返します。
clear map のすべての要素を消去します。
containsC++20 map 内に指定されたキーを持つ要素があるかどうかを確認します。
count パラメーターに指定したキーに一致するキーを持つ、map 内の要素の数を返します。
crbegin 反転された map 内の最初の要素を指す定数反復子を返します。
crend 反転された map 内の最後の要素の次の位置を指す定数反復子を返します。
emplace インプレースで構築された要素を map に挿入します。
emplace_hint インプレースで構築された要素を、配置ヒントと共に map に挿入します。
empty map が空の場合は true を返します。
end 末尾超え反復子を返します。
equal_range 反復子のペアを返します。 ペアに含まれる最初の反復子は、指定したキーよりも大きいキーを持つ、map 内の最初の要素を指します。 ペアに含まれる 2 番目の反復子は、指定したキーと等しいかそれよりも大きいキーを持つ、map 内の最初の要素を指します。
erase 指定した位置から map 内の要素または要素範囲を削除します。
find 指定したキーと同じキーを持つ、map 内の要素の位置を指す反復子を返します。
get_allocator allocator の構築に使用される map オブジェクトのコピーを返します。
insert 指定した位置において、map に単一の要素または要素の範囲を挿入します。
key_comp map 内のキーを並べ替えるために使用される比較オブジェクトのコピーを返します。
lower_bound 指定したキーと等しいかそれよりも大きいキー値を持つ、map 内の最初の要素を指す反復子を返します。
max_size map の最大長を返します。
rbegin 反転された map 内の最初の要素を指す反復子を返します。
rend 反転された map 内の最後の要素の次の位置を指す反復子を返します。
size map 内の要素数を返します。
swap 2 つの map の要素を交換します。
upper_bound 指定したキーよりも大きいキー値を持つ、map 内の最初の要素を指す反復子を返します。
value_comp map 内の要素の値を並べ替えるために使用される比較オブジェクトのコピーを取得します。

演算子

名前 説明
operator[] map に、指定したキー値を持つ要素を挿入します。
operator= 別の map のコピーで map の要素を置き換えます。

allocator_type

map オブジェクトのアロケーター クラスを表す型。

typedef Allocator allocator_type;

allocator_type の使用例については、get_allocator の例をご覧ください。

at

指定されたキー値を持つ要素を検索します。

Type& at(const Key& key);

const Type& at(const Key& key) const;

パラメーター

key
検索するキー値。

戻り値

見つかった要素のデータ値への参照。

解説

引数のキー値が見つからない場合、この関数は、out_of_range クラスのオブジェクトをスローします。

// map_at.cpp
// compile with: /EHsc
#include <map>
#include <iostream>

typedef std::map<char, int> Mymap;
int main()
    {
    Mymap c1;

    c1.insert(Mymap::value_type('a', 1));
    c1.insert(Mymap::value_type('b', 2));
    c1.insert(Mymap::value_type('c', 3));

// find and show elements
    std::cout << "c1.at('a') == " << c1.at('a') << std::endl;
    std::cout << "c1.at('b') == " << c1.at('b') << std::endl;
    std::cout << "c1.at('c') == " << c1.at('c') << std::endl;

    return (0);
    }

begin

map 内の最初の要素を指す反復子を返します。

const_iterator begin() const;

iterator begin();

戻り値

map 内の最初の要素、または空の map の次の位置を指す双方向反復子。

// map_begin.cpp
// compile with: /EHsc
#include <map>
#include <iostream>

int main( )
{
   using namespace std;
   map <int, int> m1;

   map <int, int> :: iterator m1_Iter;
   map <int, int> :: const_iterator m1_cIter;
   typedef pair <int, int> Int_Pair;

   m1.insert ( Int_Pair ( 0, 0 ) );
   m1.insert ( Int_Pair ( 1, 1 ) );
   m1.insert ( Int_Pair ( 2, 4 ) );

   m1_cIter = m1.begin ( );
   cout << "The first element of m1 is " << m1_cIter -> first << endl;

   m1_Iter = m1.begin ( );
   m1.erase ( m1_Iter );

   // The following 2 lines would err because the iterator is const
   // m1_cIter = m1.begin ( );
   // m1.erase ( m1_cIter );

   m1_cIter = m1.begin( );
   cout << "The first element of m1 is now " << m1_cIter -> first << endl;
}
The first element of m1 is 0
The first element of m1 is now 1

cbegin

範囲内の最後の要素の次の位置を指す const 反復子を返します。

const_iterator cbegin() const;

戻り値

範囲の最初の要素、または空の範囲の末尾の次の位置 (空の範囲の場合、const) を指す cbegin() == cend() 双方向反復子。

解説

cbegin の戻り値で範囲内の要素を変更することはできません。

begin() メンバー関数の代わりにこのメンバー関数を使用して、戻り値が const_iterator になることを保証できます。 通常は、次の例に示すように auto 型推論キーワードと共に使用します。 例では、Containerconstbegin() をサポートする任意の種類の変更可能な (非 cbegin()) コンテナーであると見なします。

auto i1 = Container.begin();
// i1 is Container<T>::iterator
auto i2 = Container.cbegin();

// i2 is Container<T>::const_iterator

cend

範囲内の最後の要素の次の位置を指す const 反復子を返します。

const_iterator cend() const;

戻り値

範囲の末尾の次の位置を指し示す const 双方向アクセス反復子。

解説

cend は、反復子が範囲の末尾を超えたかどうかをテストするために使用されます。

end() メンバー関数の代わりにこのメンバー関数を使用して、戻り値が const_iterator になることを保証できます。 通常は、次の例に示すように auto 型推論キーワードと共に使用します。 例では、Containerconstend() をサポートする任意の種類の変更可能な (非 cend()) コンテナーであると見なします。

auto i1 = Container.end();
// i1 is Container<T>::iterator
auto i2 = Container.cend();

// i2 is Container<T>::const_iterator

cend によって返された値は逆参照しないでください。

clear

map のすべての要素を消去します。

void clear();

map::clear メンバー関数の使用例を次に示します。

// map_clear.cpp
// compile with: /EHsc
#include <map>
#include <iostream>

int main()
{
    using namespace std;
    map<int, int> m1;
    map<int, int>::size_type i;
    typedef pair<int, int> Int_Pair;

    m1.insert(Int_Pair(1, 1));
    m1.insert(Int_Pair(2, 4));

    i = m1.size();
    cout << "The size of the map is initially "
         << i << "." << endl;

    m1.clear();
    i = m1.size();
    cout << "The size of the map after clearing is "
         << i << "." << endl;
}
The size of the map is initially 2.
The size of the map after clearing is 0.

const_iterator

const 内の 1 つの map 要素を読み取ることができる双方向反復子を提供する型。

typedef implementation-defined const_iterator;

解説

const_iterator 型で要素の値を変更することはできません。

map によって定義される const_iterator は、value_type のオブジェクトである要素を指します。これは pair<constKey, Type> 型で、その最初のメンバーは要素へのキーであり、2 番目のメンバーは要素が保持するマップされたデータです。

hash_map 内の要素を指す const_iteratorcIter を逆参照するには、-> 演算子を使用します。

要素のキーの値にアクセスするには、cIter ->first を使用します。これは (* cIter) と同等です。 first.

要素のマップされたデータの値にアクセスするには cIter ->second を使用します。これは (* cIter) と同等です。 second.

const_iterator の使用例については、begin の例をご覧ください。

const_pointer

map 内の const 要素へのポインターを提供する型。

typedef typename allocator_type::const_pointer const_pointer;

解説

const_pointer 型で要素の値を変更することはできません。

ほとんどの場合、map オブジェクト内の要素にアクセスするには、iterator を使用する必要があります。

const_reference

読み取りと const 操作を実行するために、マップに格納された const 要素への参照を提供する型。

typedef typename allocator_type::const_reference const_reference;

// map_const_ref.cpp
// compile with: /EHsc
#include <map>
#include <iostream>

int main( )
{
   using namespace std;
   map <int, int> m1;
   typedef pair <int, int> Int_Pair;

   m1.insert ( Int_Pair ( 1, 10 ) );
   m1.insert ( Int_Pair ( 2, 20 ) );

   // Declare and initialize a const_reference &Ref1
   // to the key of the first element
   const int &Ref1 = ( m1.begin( ) -> first );

   // The following line would cause an error as the
   // non-const_reference can't be used to access the key
   // int &Ref1 = ( m1.begin( ) -> first );

   cout << "The key of first element in the map is "
        << Ref1 << "." << endl;

   // Declare and initialize a reference &Ref2
   // to the data value of the first element
   int &Ref2 = ( m1.begin( ) -> second );

   cout << "The data value of first element in the map is "
        << Ref2 << "." << endl;
}
The key of first element in the map is 1.
The data value of first element in the map is 10.

const_reverse_iterator

const 内の任意の map 要素を読み取ることができる双方向反復子を提供する型。

typedef std::reverse_iterator<const_iterator> const_reverse_iterator;

解説

const_reverse_iterator 型は要素の値を変更できず、逆の順序で map を反復処理するために使用します。

map によって定義される const_reverse_iterator は、value_type のオブジェクトである要素を指します。これは pair<const Key, Type> 型で、その最初のメンバーは要素へのキーであり、2 番目のメンバーは要素が保持するマップされたデータです。

hash_map 内の要素を指す const_reverse_iterator crIter を逆参照するには、-> 演算子を使用します。

要素のキーの値にアクセスするには、crIter ->first を使用します。これは、(* crIter).first と同等です。

要素のマップされたデータの値にアクセスするには crIter ->second を使用します。これは (* crIter).first と同等です。

const_reverse_iterator の宣言や使用の方法の例については、rend の例を参照してください。

count

パラメーター指定したキーに一致するキーを持つ、map 内の要素の数を返します。

size_type count(const Key& key) const;

パラメーター

key
照合される map の要素のキー値。

戻り値

map に、並べ替えキーがパラメーター キーと一致する要素が含まれている場合は 1。map に、キーが一致する要素が含まれていない場合は 0。

解説

メンバー関数は、次の範囲内の要素 x の数を返します。

[ lower_bound(key), upper_bound(key) )

一意の連想コンテナーである map の場合、これは 0 または 1 です。

map::count メンバー関数の使用例を次に示します。

// map_count.cpp
// compile with: /EHsc
#include <map>
#include <iostream>

int main()
{
    using namespace std;
    map<int, int> m1;
    map<int, int>::size_type i;
    typedef pair<int, int> Int_Pair;

    m1.insert(Int_Pair(1, 1));
    m1.insert(Int_Pair(2, 1));
    m1.insert(Int_Pair(1, 4));
    m1.insert(Int_Pair(2, 1));

    // Keys must be unique in map, so duplicates are ignored
    i = m1.count(1);
    cout << "The number of elements in m1 with a sort key of 1 is: "
         << i << "." << endl;

    i = m1.count(2);
    cout << "The number of elements in m1 with a sort key of 2 is: "
         << i << "." << endl;

    i = m1.count(3);
    cout << "The number of elements in m1 with a sort key of 3 is: "
         << i << "." << endl;
}
The number of elements in m1 with a sort key of 1 is: 1.
The number of elements in m1 with a sort key of 2 is: 1.
The number of elements in m1 with a sort key of 3 is: 0.

contains

map 内に、指定されたキーを持つ要素があるかどうかを確認します。

bool contains(const Key& key) const;
template<class K> bool contains(const K& key) const;

パラメーター

K
キーの型。

key
探す要素のキー値。

戻り値

要素がコンテナー内で見つかった場合は true。それ以外の場合は false

解説

contains() は C++20 の新機能です。 これを使用するには、/std:c++20 以降のコンパイラ オプションを指定します。

template<class K> bool contains(const K& key) const が透過的な場合にのみ、key_compare はオーバーロードの解決に使用されます。 詳細については、「連想コンテナーの異種ルックアップ」をご覧ください。

// Requires /std:c++20 or /std:c++latest
#include <map>
#include <string>
#include <iostream>
#include <functional>

int main()
{
    std::map<int, bool> m = {{0, true},{1, false}};

    std::cout << std::boolalpha; // so booleans show as 'true' or 'false'
    std::cout << m.contains(1) << '\n';
    std::cout << m.contains(2) << '\n';

    // call template function
    std::map<std::string, int, std::less<>> m2 = {{"ten", 10}, {"twenty", 20}, {"thirty", 30}};
    std::cout << m2.contains("ten");

    return 0;
}
true
false
true

crbegin

反転された map 内の最初の要素を指す定数反復子を返します。

const_reverse_iterator crbegin() const;

戻り値

逆順の map 内の最初の要素を指す定数逆順双方向反復子 (または通常の順序の map 内の最後の要素だったものを指す定数逆順双方向反復子)。

解説

crbegin は、beginmap で使用されるのと同様に、反転された map で使用されます。

戻り値が crbegin の場合、map オブジェクトは変更できません。

crbegin を使用して、map 内を後方に向かって反復処理できます。

// map_crbegin.cpp
// compile with: /EHsc
#include <map>
#include <iostream>

int main( )
{
   using namespace std;
   map <int, int> m1;

   map <int, int> :: const_reverse_iterator m1_crIter;
   typedef pair <int, int> Int_Pair;

   m1.insert ( Int_Pair ( 1, 10 ) );
   m1.insert ( Int_Pair ( 2, 20 ) );
   m1.insert ( Int_Pair ( 3, 30 ) );

   m1_crIter = m1.crbegin( );
   cout << "The first element of the reversed map m1 is "
        << m1_crIter -> first << "." << endl;
}
The first element of the reversed map m1 is 3.

crend

反転された map 内の最後の要素の次の位置を指す定数反復子を返します。

const_reverse_iterator crend() const;

戻り値

逆順の map 内の最後の要素の次の場所 (通常の順序の map 内の最初の要素の前の場所) を指す定数逆順双方向反復子。

解説

crend は、endmap で使用されるのと同様に、反転された map で使用されます。

戻り値が crend の場合、map オブジェクトは変更できません。

crend を使用して、逆順反復子が map の末尾に達したかどうかをテストできます。

crend によって返された値は逆参照しないでください。

// map_crend.cpp
// compile with: /EHsc
#include <map>
#include <iostream>

int main( )
{
   using namespace std;
   map <int, int> m1;

   map <int, int> :: const_reverse_iterator m1_crIter;
   typedef pair <int, int> Int_Pair;

   m1.insert ( Int_Pair ( 1, 10 ) );
   m1.insert ( Int_Pair ( 2, 20 ) );
   m1.insert ( Int_Pair ( 3, 30 ) );

   m1_crIter = m1.crend( );
   m1_crIter--;
   cout << "The last element of the reversed map m1 is "
        << m1_crIter -> first << "." << endl;
}
The last element of the reversed map m1 is 1.

difference_type

反復子が指す要素の範囲内にある map の要素の数を表すのに使用できる符号付き整数型。

typedef allocator_type::difference_type difference_type;

解説

difference_type は、コンテナーの反復子を減算またはインクリメントするときに返される型です。 通常 difference_type は、[ first, last) の範囲内で、反復子 firstlast の間にある要素の数を表すために使用され、first が指す要素と、last が指す要素の 1 つ前までの範囲の要素を含みます。

difference_type は、入力反復子の要件を満たすすべての反復子 (set などの反転可能なコンテナーによってサポートされる双方向反復子のクラスを含む) に対して使用できますが、反復子間の減算は、vector などのランダム アクセス コンテナーによって提供される、ランダム アクセス反復子によってのみサポートされます。

// map_diff_type.cpp
// compile with: /EHsc
#include <iostream>
#include <map>
#include <algorithm>

int main( )
{
   using namespace std;
   map <int, int> m1;
   typedef pair <int, int> Int_Pair;

   m1.insert ( Int_Pair ( 2, 20 ) );
   m1.insert ( Int_Pair ( 1, 10 ) );
   m1.insert ( Int_Pair ( 3, 20 ) );
   m1.insert ( Int_Pair ( 2, 30 ) );

   map <int, int>::iterator m1_Iter, m1_bIter, m1_eIter;
   m1_bIter = m1.begin( );
   m1_eIter = m1.end( );

   // Count the number of elements in a map
   map <int, int>::difference_type  df_count = 1;
   m1_Iter = m1.begin( );
   while ( m1_Iter != m1_eIter)
   {
      df_count++;
      m1_Iter++;
   }

   cout << "The number of elements in the map m1 is: "
        << df_count << "." << endl;
}
The number of elements in the map m1 is: 4.

emplace

インプレースで構築された (コピーまたは移動操作が実行されない) 要素をマップに挿入します。

template <class... Args>
pair<iterator, bool>
emplace(
    Args&&... args);

パラメーター

args
値が同じ順序付けになる要素がマップにまだ含まれていない場合に、マップに挿入する要素を構築するために転送される引数。

戻り値

1 つの pair で、その bool コンポーネントは、挿入が行われた場合は true を返し、map に既に順序の値が等しい要素が含まれている場合は false を返します。 戻り値であるペアの反復子コンポーネントは、bool コンポーネントが true の場合は新しく挿入される要素を指し、bool コンポーネントが false の場合は既存の要素を指します。

pairpr の反復子コンポーネントにアクセスするには、pr.first を使用します。これを逆参照するには、*pr.first を使用します。 bool コンポーネントにアクセスするには、pr.second を使用します。 例については、この記事で後ほど説明するサンプル コードを参照してください。

解説

この関数では、反復子や参照は無効になりません。

配置の実行中、例外がスローされるとコンテナーの状態は変更されません。

要素の value_type はペアです。最初のコンポーネントがキー値と等しく、2 番目のコンポーネントが要素のデータ値と等しくなるよう、要素の値が順序付けされたペアになります。

// map_emplace.cpp
// compile with: /EHsc
#include <map>
#include <string>
#include <iostream>

using namespace std;

template <typename M> void print(const M& m) {
    cout << m.size() << " elements: ";

    for (const auto& p : m) {
        cout << "(" << p.first << ", " << p.second << ") ";
    }

    cout << endl;
}

int main()
{
    map<int, string> m1;

    auto ret = m1.emplace(10, "ten");

    if (!ret.second){
        auto pr = *ret.first;
        cout << "Emplace failed, element with key 10 already exists."
            << endl << "  The existing element is (" << pr.first << ", " << pr.second << ")"
            << endl;
        cout << "map not modified" << endl;
    }
    else{
        cout << "map modified, now contains ";
        print(m1);
    }
    cout << endl;

    ret = m1.emplace(10, "one zero");

    if (!ret.second){
        auto pr = *ret.first;
        cout << "Emplace failed, element with key 10 already exists."
            << endl << "  The existing element is (" << pr.first << ", " << pr.second << ")"
            << endl;
    }
    else{
        cout << "map modified, now contains ";
        print(m1);
    }
    cout << endl;
}

emplace_hint

インプレースで構築された (コピーまたは移動操作が実行されない) 要素を、配置ヒントと一緒に挿入します。

template <class... Args>
iterator emplace_hint(
    const_iterator where,
    Args&&... args);

パラメーター

args
挿入される要素をマップがまだ含んでいない場合、より一般的には、キーが同じ順序付けになる要素がマップにまだ含まれていない場合に、マップに挿入する要素を構築するために転送される引数。

where
正しい挿入ポイントの検索を開始する場所 (その位置が where の直前にある場合、挿入処理は対数時間ではなく償却定数時間で実行できます)。

戻り値

新しく挿入される要素を指す反復子。

要素が既に存在するために挿入が失敗した場合は、既存の要素への反復子を要素のキーと共に返します。

解説

この関数では、反復子や参照は無効になりません。

配置の実行中、例外がスローされるとコンテナーの状態は変更されません。

要素の value_type はペアです。最初のコンポーネントがキー値と等しく、2 番目のコンポーネントが要素のデータ値と等しくなるよう、要素の値が順序付けされたペアになります。

// map_emplace.cpp
// compile with: /EHsc
#include <map>
#include <string>
#include <iostream>

using namespace std;

template <typename M> void print(const M& m) {
    cout << m.size() << " elements: " << endl;

    for (const auto& p : m) {
        cout << "(" << p.first <<  "," << p.second << ") ";
    }

    cout << endl;
}

int main()
{
    map<string, string> m1;

    // Emplace some test data
    m1.emplace("Anna", "Accounting");
    m1.emplace("Bob", "Accounting");
    m1.emplace("Carmine", "Engineering");

    cout << "map starting data: ";
    print(m1);
    cout << endl;

    // Emplace with hint
    // m1.end() should be the "next" element after this emplacement
    m1.emplace_hint(m1.end(), "Doug", "Engineering");

    cout << "map modified, now contains ";
    print(m1);
    cout << endl;
}

empty

map が空かどうかをテストします。

bool empty() const;

戻り値

map が空の場合は true、map が空ではない場合は false

// map_empty.cpp
// compile with: /EHsc
#include <map>
#include <iostream>

int main( )
{
   using namespace std;
   map <int, int> m1, m2;

   typedef pair <int, int> Int_Pair;
   m1.insert ( Int_Pair ( 1, 1 ) );

   if ( m1.empty( ) )
      cout << "The map m1 is empty." << endl;
   else
      cout << "The map m1 is not empty." << endl;

   if ( m2.empty( ) )
      cout << "The map m2 is empty." << endl;
   else
      cout << "The map m2 is not empty." << endl;
}
The map m1 is not empty.
The map m2 is empty.

end

末尾超え反復子を返します。

const_iterator end() const;

iterator end();

戻り値

末尾超え反復子。 map が空の場合は、map::end() == map::begin()

解説

end は、反復子が map の末尾を超えたかどうかをテストするために使用します。

end によって返された値は逆参照しないでください。

コード例については、「map::find」を参照してください。

equal_range

キーの lower_bound とキーの upper_bound を表す反復子のペアを返します。

pair <const_iterator, const_iterator> equal_range (const Key& key) const;

pair <iterator, iterator> equal_range (const Key& key);

パラメーター

key
検索対象の map 内の要素の並べ替えキーと比較される引数キー値。

戻り値

このメンバー関数によって返されるペア pr の 1 番目の反復子にアクセスするには、pr. 最初に、下限反復子を逆参照するには、*( を使用します pr1 つ目)。 このメンバー関数によって返されるペア pr の 2 番目の反復子にアクセスするには、pr. に、上限の反復子を逆参照するには、*( を使用します pr2 番目)。

// map_equal_range.cpp
// compile with: /EHsc
#include <map>
#include <iostream>

int main( )
{
   using namespace std;
   typedef map <int, int, less<int> > IntMap;
   IntMap m1;
   map <int, int> :: const_iterator m1_RcIter;
   typedef pair <int, int> Int_Pair;

   m1.insert ( Int_Pair ( 1, 10 ) );
   m1.insert ( Int_Pair ( 2, 20 ) );
   m1.insert ( Int_Pair ( 3, 30 ) );

   pair <IntMap::const_iterator, IntMap::const_iterator> p1, p2;
   p1 = m1.equal_range( 2 );

   cout << "The lower bound of the element with "
        << "a key of 2 in the map m1 is: "
        << p1.first -> second << "." << endl;

   cout << "The upper bound of the element with "
        << "a key of 2 in the map m1 is: "
        << p1.second -> second << "." << endl;

   // Compare the upper_bound called directly
   m1_RcIter = m1.upper_bound( 2 );

   cout << "A direct call of upper_bound( 2 ) gives "
        << m1_RcIter -> second << "," << endl
        << "matching the 2nd element of the pair"
        << " returned by equal_range( 2 )." << endl;

   p2 = m1.equal_range( 4 );

   // If no match is found for the key,
   // both elements of the pair return end( )
   if ( ( p2.first == m1.end( ) ) && ( p2.second == m1.end( ) ) )
      cout << "The map m1 doesn't have an element "
           << "with a key less than 40." << endl;
   else
      cout << "The element of map m1 with a key >= 40 is: "
           << p2.first -> first << "." << endl;
}
The lower bound of the element with a key of 2 in the map m1 is: 20.
The upper bound of the element with a key of 2 in the map m1 is: 30.
A direct call of upper_bound( 2 ) gives 30,
matching the 2nd element of the pair returned by equal_range( 2 ).
The map m1 doesn't have an element with a key less than 40.

erase

マップ内の要素または要素の範囲を指定した位置から削除するか、または指定したキーと一致する要素を削除します。

iterator erase(
    const_iterator Where);

iterator erase(
    const_iterator First,
    const_iterator Last);

size_type erase(
    const key_type& Key);

パラメーター

Where
削除される要素の位置。

First
削除される最初の要素の位置。

Last
削除される最後の要素の次の位置。

Key
削除される要素のキー値。

戻り値

最初の 2 つのメンバー関数の場合は、削除された要素の後の最初の残存要素、またはマップの最後の要素 (このような要素が存在しない場合) を指定する双方向反復子。

3 番目のメンバー関数の場合は、マップから削除された要素の数を返します。

// map_erase.cpp
// compile with: /EHsc
#include <map>
#include <string>
#include <iostream>
#include <iterator> // next() and prev() helper functions
#include <utility>  // make_pair()

using namespace std;

using mymap = map<int, string>;

void printmap(const mymap& m) {
    for (const auto& elem : m) {
        cout << " [" << elem.first << ", " << elem.second << "]";
    }
    cout << endl << "size() == " << m.size() << endl << endl;
}

int main()
{
    mymap m1;

    // Fill in some data to test with, one at a time
    m1.insert(make_pair(1, "A"));
    m1.insert(make_pair(2, "B"));
    m1.insert(make_pair(3, "C"));
    m1.insert(make_pair(4, "D"));
    m1.insert(make_pair(5, "E"));

    cout << "Starting data of map m1 is:" << endl;
    printmap(m1);
    // The 1st member function removes an element at a given position
    m1.erase(next(m1.begin()));
    cout << "After the 2nd element is deleted, the map m1 is:" << endl;
    printmap(m1);

    // Fill in some data to test with, one at a time, using an initializer list
    mymap m2
    {
        { 10, "Bob" },
        { 11, "Rob" },
        { 12, "Robert" },
        { 13, "Bert" },
        { 14, "Bobby" }
    };

    cout << "Starting data of map m2 is:" << endl;
    printmap(m2);
    // The 2nd member function removes elements
    // in the range [First, Last)
    m2.erase(next(m2.begin()), prev(m2.end()));
    cout << "After the middle elements are deleted, the map m2 is:" << endl;
    printmap(m2);

    mymap m3;

    // Fill in some data to test with, one at a time, using emplace
    m3.emplace(1, "red");
    m3.emplace(2, "yellow");
    m3.emplace(3, "blue");
    m3.emplace(4, "green");
    m3.emplace(5, "orange");
    m3.emplace(6, "purple");
    m3.emplace(7, "pink");

    cout << "Starting data of map m3 is:" << endl;
    printmap(m3);
    // The 3rd member function removes elements with a given Key
    mymap::size_type count = m3.erase(2);
    // The 3rd member function also returns the number of elements removed
    cout << "The number of elements removed from m3 is: " << count << "." << endl;
    cout << "After the element with a key of 2 is deleted, the map m3 is:" << endl;
    printmap(m3);
}

find

指定したキーと等価のキーを持つ、map 内の要素の位置を参照する反復子を返します。

iterator find(const Key& key);

const_iterator find(const Key& key) const;

パラメーター

key
検索対象の map 内の要素の並べ替えキーによって照合されるキー値。

戻り値

指定したキーを持つ要素の位置を参照する反復子。キーの一致が検出されない場合は、set 内の最後の要素の次の位置 map (map::end())。

解説

このメンバー関数は、小なり比較関係に基づいて順序を推論する二項述語に即して、並べ替えキーが引数キーと等価である map 内の要素を参照する反復子を返します。

find の戻り値が const_iterator に割り当てられている場合、map オブジェクトは変更できません。 find の戻り値が iterator に割り当てられている場合、map オブジェクトを変更できます。

// compile with: /EHsc /W4 /MTd
#include <map>
#include <iostream>
#include <vector>
#include <string>
#include <utility>  // make_pair()

using namespace std;

template <typename A, typename B> void print_elem(const pair<A, B>& p) {
    cout << "(" << p.first << ", " << p.second << ") ";
}

template <typename T> void print_collection(const T& t) {
    cout << t.size() << " elements: ";

    for (const auto& p : t) {
        print_elem(p);
    }
    cout << endl;
}

template <typename C, class T> void findit(const C& c, T val) {
    cout << "Trying find() on value " << val << endl;
    auto result = c.find(val);
    if (result != c.end()) {
        cout << "Element found: "; print_elem(*result); cout << endl;
    } else {
        cout << "Element not found." << endl;
    }
}

int main()
{
    map<int, string> m1({ { 40, "Zr" }, { 45, "Rh" } });
    cout << "The starting map m1 is (key, value):" << endl;
    print_collection(m1);

    vector<pair<int, string>> v;
    v.push_back(make_pair(43, "Tc"));
    v.push_back(make_pair(41, "Nb"));
    v.push_back(make_pair(46, "Pd"));
    v.push_back(make_pair(42, "Mo"));
    v.push_back(make_pair(44, "Ru"));
    v.push_back(make_pair(44, "Ru")); // attempt a duplicate

    cout << "Inserting the following vector data into m1:" << endl;
    print_collection(v);

    m1.insert(v.begin(), v.end());

    cout << "The modified map m1 is (key, value):" << endl;
    print_collection(m1);
    cout << endl;
    findit(m1, 45);
    findit(m1, 6);
}

get_allocator

map の構築に使用されるアロケーター オブジェクトのコピーを返します。

allocator_type get_allocator() const;

戻り値

map で使用されるアロケーター。

解説

map クラスのアロケーターは、クラスがどのようにストレージを管理するかを指定します。 C++ 標準ライブラリ コンテナー クラスで提供される既定のアロケーターは、ほとんどのプログラミング要件に対応しています。 独自のアロケーター クラスを作成して使用することは、C++ における高度な作業の 1 つです。

// map_get_allocator.cpp
// compile with: /EHsc
#include <map>
#include <iostream>

int main( )
{
   using namespace std;
   map <int, int>::allocator_type m1_Alloc;
   map <int, int>::allocator_type m2_Alloc;
   map <int, double>::allocator_type m3_Alloc;
   map <int, int>::allocator_type m4_Alloc;

   // The following lines declare objects
   // that use the default allocator.
   map <int, int> m1;
   map <int, int, allocator<int> > m2;
   map <int, double, allocator<double> > m3;

   m1_Alloc = m1.get_allocator( );
   m2_Alloc = m2.get_allocator( );
   m3_Alloc = m3.get_allocator( );

   cout << "The number of integers that can be allocated\n"
        << "before free memory is exhausted: "
        << m2.max_size( ) << ".\n" << endl;

   cout << "The number of doubles that can be allocated\n"
        << "before free memory is exhausted: "
        << m3.max_size( ) <<  ".\n" << endl;

   // The following line creates a map m4
   // with the allocator of map m1.
   map <int, int> m4( less<int>( ), m1_Alloc );

   m4_Alloc = m4.get_allocator( );

   // Two allocators are interchangeable if
   // storage allocated from each can be
   // deallocated with the other
   if( m1_Alloc == m4_Alloc )
   {
      cout << "The allocators are interchangeable." << endl;
   }
   else
   {
      cout << "The allocators are not interchangeable." << endl;
   }
}

insert

map に要素または要素範囲を挿入します。

// (1) single element
pair<iterator, bool> insert(
    const value_type& Val);

// (2) single element, perfect forwarded
template <class ValTy>
pair<iterator, bool>
insert(
    ValTy&& Val);

// (3) single element with hint
iterator insert(
    const_iterator Where,
    const value_type& Val);

// (4) single element, perfect forwarded, with hint
template <class ValTy>
iterator insert(
    const_iterator Where,
    ValTy&& Val);

// (5) range
template <class InputIterator>
void insert(
    InputIterator First,
    InputIterator Last);

// (6) initializer list
void insert(
    initializer_list<value_type>
IList);

パラメーター

Val
キーが同じ順序付けになる要素が map にまだ含まれていない場合に、map に挿入される要素の値。

Where
正しい挿入ポイントの検索を開始する場所 (その位置が Where の直前にある場合、挿入処理は対数時間ではなく償却定数時間で実行できます)。

ValTy
map が value_type の要素を構築するために使用できる引数の型を指定し、Val を引数として完全転送するテンプレート パラメーター。

First
コピーされる最初の要素の位置。

Last
コピーされる最後の要素の次の位置。

InputIterator
入力反復子の要件を満たすテンプレート関数の引数。これは、value_type オブジェクトの構築に使用できる型の要素を指し示します。

IList
要素のコピー元の initializer_list

戻り値

単一要素のメンバー関数 (1) と (2) は、pair を返し、このペアの bool コンポーネントは、挿入が行われた場合は true になり、順序の値が同じキーを持つ要素が map に既に含まれている場合は false になります。 戻り値であるペアの反復子コンポーネントは、bool コンポーネントが true の場合は新しく挿入される要素を指し、bool コンポーネントが false の場合は既存の要素を指します。

単一要素とヒントのメンバー関数 (3) と (4) は、map に挿入された新しい要素の位置を指す反復子を返します。ただし、同じキーを持つ要素が既に存在する場合、この反復子は既存の要素を指します。

解説

この関数では、反復子、ポインター、参照は無効になりません。

要素を 1 つだけ挿入するとき、例外がスローされるとコンテナーの状態は変更されません。 複数の要素を挿入するときに例外がスローされた場合、コンテナーの状態は未指定ですが、有効な状態になっています。

単一要素のメンバー関数によって返される pairpr の反復子コンポーネントにアクセスするには、pr.first を使用します。返されるペアに含まれる反復子を逆参照するには、要素を指定して、*pr.first を使用します。 bool コンポーネントにアクセスするには、pr.second を使用します。 例については、この記事で後ほど説明するサンプル コードを参照してください。

コンテナーの value_type はそのコンテナーに属する typedef であり、map の場合、map<K, V>::value_typepair<const K, V> です。 要素の値は順序付けされたペアになり、このペアの最初のコンポーネントはキー値と同じで、2 番目のコンポーネントは要素のデータ値と同じになります。

範囲のメンバー関数 (5) は、map に要素値のシーケンスを挿入します。このシーケンスは、範囲 [First, Last) の反復子によってアドレス指定された各要素に対応します。したがって、Last は挿入されません。 コンテナーのメンバー関数 end() は、コンテナー内にある最後の要素の直後の位置を参照します。たとえば、ステートメント m.insert(v.begin(), v.end()); は、v のすべての要素を m に挿入しようとします。 範囲内で一意の値を持つ要素だけが挿入されますが、値が重複する要素は無視されます。 拒否される要素を確認するには、1 つの要素が指定された insert を使用します。

初期化子リストのメンバー関数 (6) は、initializer_list を使用して map に要素をコピーします。

インプレースで構築された (つまり、コピーまたは移動操作が実行されない) 要素の挿入については、「map::emplace」および「map::emplace_hint」を参照してください。

// map_insert.cpp
// compile with: /EHsc
#include <map>
#include <iostream>
#include <string>
#include <vector>
#include <utility>  // make_pair()

using namespace std;

template <typename M> void print(const M& m) {
    cout << m.size() << " elements: ";

    for (const auto& p : m) {
        cout << "(" << p.first << ", " << p.second << ") ";
    }

    cout << endl;
}

int main()
{
    // insert single values
    map<int, int> m1;
    // call insert(const value_type&) version
    m1.insert({ 1, 10 });
    // call insert(ValTy&&) version
    m1.insert(make_pair(2, 20));

    cout << "The original key and mapped values of m1 are:" << endl;
    print(m1);

    // intentionally attempt a duplicate, single element
    auto ret = m1.insert(make_pair(1, 111));
    if (!ret.second){
        auto pr = *ret.first;
        cout << "Insert failed, element with key value 1 already exists."
            << endl << "  The existing element is (" << pr.first << ", " << pr.second << ")"
            << endl;
    }
    else{
        cout << "The modified key and mapped values of m1 are:" << endl;
        print(m1);
    }
    cout << endl;

    // single element, with hint
    m1.insert(m1.end(), make_pair(3, 30));
    cout << "The modified key and mapped values of m1 are:" << endl;
    print(m1);
    cout << endl;

    // The templatized version inserting a jumbled range
    map<int, int> m2;
    vector<pair<int, int>> v;
    v.push_back(make_pair(43, 294));
    v.push_back(make_pair(41, 262));
    v.push_back(make_pair(45, 330));
    v.push_back(make_pair(42, 277));
    v.push_back(make_pair(44, 311));

    cout << "Inserting the following vector data into m2:" << endl;
    print(v);

    m2.insert(v.begin(), v.end());

    cout << "The modified key and mapped values of m2 are:" << endl;
    print(m2);
    cout << endl;

    // The templatized versions move-constructing elements
    map<int, string>  m3;
    pair<int, string> ip1(475, "blue"), ip2(510, "green");

    // single element
    m3.insert(move(ip1));
    cout << "After the first move insertion, m3 contains:" << endl;
    print(m3);

    // single element with hint
    m3.insert(m3.end(), move(ip2));
    cout << "After the second move insertion, m3 contains:" << endl;
    print(m3);
    cout << endl;

    map<int, int> m4;
    // Insert the elements from an initializer_list
    m4.insert({ { 4, 44 }, { 2, 22 }, { 3, 33 }, { 1, 11 }, { 5, 55 } });
    cout << "After initializer_list insertion, m4 contains:" << endl;
    print(m4);
    cout << endl;
}

iterator

map 内の任意の要素の読み取りまたは変更ができる双方向反復子を提供する型。

typedef implementation-defined iterator;

解説

map によって定義される iterator は value_type のオブジェクトである要素を指します。これは pair<const Key, Type> 型で、その最初のメンバーは要素へのキーであり、2 番目のメンバーは要素が保持するマップされたデータです。

map 内の要素を指す反復子 Iter を逆参照するには、-> 演算子を使用します。

要素のキーの値にアクセスするには、Iter->first を使用します。これは、(*Iter).first と同等です。 要素のマップされたデータの値にアクセスするには Iter->second を使用します。これは (*Iter).second と同等です。

iterator の宣言方法や使用方法の例については、begin の例を参照してください。

key_comp

map 内のキーの並べ替えに使用する比較オブジェクトのコピーを取得します。

key_compare key_comp() const;

戻り値

map が要素の並べ替えに使用する関数オブジェクトを返します。

解説

格納されているオブジェクトは以下のメンバー関数を定義します。

bool operator(const Key& left, const Key& right);

これは、並べ替え順で leftright に先行しかつ等しくない場合に true を返します。

// map_key_comp.cpp
// compile with: /EHsc
#include <map>
#include <iostream>

int main( )
{
   using namespace std;

   map <int, int, less<int> > m1;
   map <int, int, less<int> >::key_compare kc1 = m1.key_comp( ) ;
   bool result1 = kc1( 2, 3 ) ;
   if( result1 == true )
   {
      cout << "kc1( 2,3 ) returns value of true, "
           << "where kc1 is the function object of m1."
           << endl;
   }
   else
   {
      cout << "kc1( 2,3 ) returns value of false "
           << "where kc1 is the function object of m1."
           << endl;
   }

   map <int, int, greater<int> > m2;
   map <int, int, greater<int> >::key_compare kc2 = m2.key_comp( );
   bool result2 = kc2( 2, 3 ) ;
   if( result2 == true )
   {
      cout << "kc2( 2,3 ) returns value of true, "
           << "where kc2 is the function object of m2."
           << endl;
   }
   else
   {
      cout << "kc2( 2,3 ) returns value of false, "
           << "where kc2 is the function object of m2."
           << endl;
   }
}
kc1( 2,3 ) returns value of true, where kc1 is the function object of m1.
kc2( 2,3 ) returns value of false, where kc2 is the function object of m2.

key_compare

2 つの並べ替えキーを比較して、map 内の 2 つの要素の相対順序を決定できる関数オブジェクトを提供する型。

typedef Traits key_compare;

解説

key_compare は、テンプレート パラメーター Traits のシノニムです。

Traits の詳細については、map クラスのトピックをご覧ください。

key_compare の宣言方法や使用方法の例については、key_comp の例を参照してください。

key_type

map の各要素に格納されている並べ替えキーを表す型。

typedef Key key_type;

解説

key_type は、テンプレート パラメーター Key のシノニムです。

Key の詳細については、map クラスのトピックの「注釈」のセクションをご覧ください。

key_type の宣言方法や使用方法の例については、value_type の例を参照してください。

lower_bound

指定したキー以上のキー値を持つ、map 内の最初の要素を指す反復子を返します。

iterator lower_bound(const Key& key);

const_iterator lower_bound(const Key& key) const;

パラメーター

key
検索対象の map 内の要素の並べ替えキーと比較される引数キー値。

戻り値

引数キー以上のキーを持つ map 内の要素の位置を指す、または、キーの一致が検出されない場合は map 内の最後の要素の次の位置を指す、iterator または const_iterator

lower_bound の戻り値が const_iterator に割り当てられている場合、map オブジェクトは変更できません。 lower_bound の戻り値が iterator に割り当てられている場合、map オブジェクトを変更できます。

// map_lower_bound.cpp
// compile with: /EHsc
#include <map>
#include <iostream>

int main( )
{
   using namespace std;
   map <int, int> m1;
   map <int, int> :: const_iterator m1_AcIter, m1_RcIter;
   typedef pair <int, int> Int_Pair;

   m1.insert ( Int_Pair ( 1, 10 ) );
   m1.insert ( Int_Pair ( 2, 20 ) );
   m1.insert ( Int_Pair ( 3, 30 ) );

   m1_RcIter = m1.lower_bound( 2 );
   cout << "The first element of map m1 with a key of 2 is: "
        << m1_RcIter -> second << "." << endl;

   // If no match is found for this key, end( ) is returned
   m1_RcIter = m1. lower_bound ( 4 );

   if ( m1_RcIter == m1.end( ) )
      cout << "The map m1 doesn't have an element "
           << "with a key of 4." << endl;
   else
      cout << "The element of map m1 with a key of 4 is: "
           << m1_RcIter -> second << "." << endl;

   // The element at a specific location in the map can be found
   // using a dereferenced iterator addressing the location
   m1_AcIter = m1.end( );
   m1_AcIter--;
   m1_RcIter = m1. lower_bound ( m1_AcIter -> first );
   cout << "The element of m1 with a key matching "
        << "that of the last element is: "
        << m1_RcIter -> second << "." << endl;
}
The first element of map m1 with a key of 2 is: 20.
The map m1 doesn't have an element with a key of 4.
The element of m1 with a key matching that of the last element is: 30.

map

空のマップを構築するか、他のマップの全体または一部のコピーであるマップを構築します。

map();

explicit map(
    const Traits& Comp);

map(
    const Traits& Comp,
    const Allocator& Al);

map(
    const map& Right);

map(
    map&& Right);

map(
    initializer_list<value_type> IList);

map(
    initializer_list<value_type> IList,
    const Traits& Comp);

map(
    initializer_list<value_type> IList,
    const Traits& Comp,
    const Allocator& Allocator);

template <class InputIterator>
map(
    InputIterator First,
    InputIterator Last);

template <class InputIterator>
map(
    InputIterator First,
    InputIterator Last,
    const Traits& Comp);

template <class InputIterator>
map(
    InputIterator First,
    InputIterator Last,
    const Traits& Comp,
    const Allocator& Al);

パラメーター

Al
このマップ オブジェクトに使用するストレージ アロケーター クラス。既定では、Allocator です。

Comp
const Traits 内の要素の並べ替えに使用される、map 型の比較関数。既定では hash_compare です。

Right
構築される map のコピー元となる map。

First
コピーする要素範囲内の最初の要素の位置。

Last
コピーする要素範囲を超える最初の要素の位置。

IList
要素のコピー元の initializer_list。

解説

すべてのコンストラクターは、アロケーター オブジェクトの型を格納します。このオブジェクトは map のメモリ ストレージを管理し、後で get_allocator を呼び出して取得することができます。 代替アロケーターの代わりに使用されるクラス宣言やプリプロセス マクロでは、アロケーターのパラメーターが省略される場合があります。

すべてのコンストラクターは、それぞれのマップを初期化します。

すべてのコンストラクターは、Traits 型の関数オブジェクトを格納します。このオブジェクトは map のキーの順序を確立するために使用され、後で key_comp を呼び出して取得することができます。

最初の 3 つのコンストラクターは、空の初期 map を指定します。2 番目のコンストラクターは要素の順序を確立するために使用する比較関数の型 (Comp) を指定し、3 番目のコンストラクターは使用するアロケーターの型 (Al) を明示的に指定します。 キーワード explicit は、特定の種類の自動型変換が実行されないようにします。

4 番目のコンストラクターは、マップ Right のコピーを指定します。

5 つ目のコンストラクターは、Right を移動することによるマップのコピーを指定します。

6 番目、7 番目、8 番目のコンストラクターは、メンバーのコピー元の initializer_list を使用します。

次の 3 つのコンストラクターは、map の範囲 [First, Last) をコピーします。下のコンストラクターになるほど、より明確に Traits クラスの比較関数と Allocator の型が指定されています。

// map_map.cpp
// compile with: /EHsc
#include <map>
#include <iostream>

int main()
{
    using namespace std;
    typedef pair <int, int> Int_Pair;
    map <int, int>::iterator m1_Iter, m3_Iter, m4_Iter, m5_Iter, m6_Iter, m7_Iter;
    map <int, int, less<int> >::iterator m2_Iter;

    // Create an empty map m0 of key type integer
    map <int, int> m0;

    // Create an empty map m1 with the key comparison
    // function of less than, then insert 4 elements
    map <int, int, less<int> > m1;
    m1.insert(Int_Pair(1, 10));
    m1.insert(Int_Pair(2, 20));
    m1.insert(Int_Pair(3, 30));
    m1.insert(Int_Pair(4, 40));

    // Create an empty map m2 with the key comparison
    // function of greater than, then insert 2 elements
    map <int, int, less<int> > m2;
    m2.insert(Int_Pair(1, 10));
    m2.insert(Int_Pair(2, 20));

    // Create a map m3 with the
    // allocator of map m1
    map <int, int>::allocator_type m1_Alloc;
    m1_Alloc = m1.get_allocator();
    map <int, int> m3(less<int>(), m1_Alloc);
    m3.insert(Int_Pair(3, 30));

    // Create a copy, map m4, of map m1
    map <int, int> m4(m1);

    // Create a map m5 by copying the range m1[ first,  last)
    map <int, int>::const_iterator m1_bcIter, m1_ecIter;
    m1_bcIter = m1.begin();
    m1_ecIter = m1.begin();
    m1_ecIter++;
    m1_ecIter++;
    map <int, int> m5(m1_bcIter, m1_ecIter);

    // Create a map m6 by copying the range m4[ first,  last)
    // and with the allocator of map m2
    map <int, int>::allocator_type m2_Alloc;
    m2_Alloc = m2.get_allocator();
    map <int, int> m6(m4.begin(), ++m4.begin(), less<int>(), m2_Alloc);

    cout << "m1 =";
    for (auto i : m1)
        cout << i.first << " " << i.second << ", ";
    cout << endl;

    cout << "m2 =";
    for(auto i : m2)
        cout << i.first << " " << i.second << ", ";
    cout << endl;

    cout << "m3 =";
    for (auto i : m3)
        cout << i.first << " " << i.second << ", ";
    cout << endl;

    cout << "m4 =";
    for (auto i : m4)
        cout << i.first << " " << i.second << ", ";
    cout << endl;

    cout << "m5 =";
    for (auto i : m5)
        cout << i.first << " " << i.second << ", ";
    cout << endl;

    cout << "m6 =";
    for (auto i : m6)
        cout << i.first << " " << i.second << ", ";
    cout << endl;

    // Create a map m7 by moving m5
    cout << "m7 =";
    map<int, int> m7(move(m5));
    for (auto i : m7)
        cout << i.first << " " << i.second << ", ";
    cout << endl;

    // Create a map m8 by copying in an initializer_list
    map<int, int> m8{ { { 1, 1 }, { 2, 2 }, { 3, 3 }, { 4, 4 } } };
    cout << "m8: = ";
    for (auto i : m8)
        cout << i.first << " " << i.second << ", ";
    cout << endl;

    // Create a map m9 with an initializer_list and a comparator
    map<int, int> m9({ { 5, 5 }, { 6, 6 }, { 7, 7 }, { 8, 8 } }, less<int>());
    cout << "m9: = ";
    for (auto i : m9)
        cout << i.first << " " << i.second << ", ";
    cout << endl;

    // Create a map m10 with an initializer_list, a comparator, and an allocator
    map<int, int> m10({ { 9, 9 }, { 10, 10 }, { 11, 11 }, { 12, 12 } }, less<int>(), m9.get_allocator());
    cout << "m10: = ";
    for (auto i : m10)
        cout << i.first << " " << i.second << ", ";
    cout << endl;
}

mapped_type

map に格納されているデータを表す型。

typedef Type mapped_type;

解説

mapped_type 型は、このクラスの Type テンプレート パラメーターの同意語です。

Type の詳細については、map クラスのトピックをご覧ください。

mapped_type の宣言方法や使用方法の例については、value_type の例を参照してください。

max_size

map の最大長を返します。

size_type max_size() const;

戻り値

map の可能な最大長。

// map_max_size.cpp
// compile with: /EHsc
#include <map>
#include <iostream>

int main( )
{
   using namespace std;
   map <int, int> m1;
   map <int, int> :: size_type i;

   i = m1.max_size( );
   cout << "The maximum possible length "
        << "of the map is " << i << "."
        << endl << "(Magnitude is machine specific.)";
}

operator[]

map に、指定したキー値を持つ要素を挿入します。

Type& operator[](const Key& key);

Type& operator[](Key&& key);

パラメーター

key
挿入する要素のキー値。

戻り値

挿入される要素のデータ値への参照。

解説

引数のキー値が見つからない場合は、データ型の既定値と一緒に挿入されます。

operator[] は、m[key] = DataValue; を使用して map m に要素を挿入するために使用できます。DataValue は、キー値が key である要素の mapped_type の値です。

operator[] を使用して要素を挿入した場合、返される参照では、挿入によって既存の要素が変更される、または新しい要素が作成されるかどうかは指示されません。 メンバー関数 findinsert を使用して、指定されたキーを持つ要素が挿入前に既に存在するかどうかを確認できます。

// map_op_insert.cpp
// compile with: /EHsc
#include <map>
#include <iostream>
#include <string>

int main( )
{
   using namespace std;
   typedef pair <const int, int> cInt2Int;
   map <int, int> m1;
   map <int, int> :: iterator pIter;

   // Insert a data value of 10 with a key of 1
   // into a map using the operator[] member function
   m1[ 1 ] = 10;

   // Compare other ways to insert objects into a map
   m1.insert ( map <int, int> :: value_type ( 2, 20 ) );
   m1.insert ( cInt2Int ( 3, 30 ) );

   cout  << "The keys of the mapped elements are:";
   for ( pIter = m1.begin( ) ; pIter != m1.end( ) ; pIter++ )
      cout << " " << pIter -> first;
   cout << "." << endl;

   cout  << "The values of the mapped elements are:";
   for ( pIter = m1.begin( ) ; pIter != m1.end( ) ; pIter++ )
      cout << " " << pIter -> second;
   cout << "." << endl;

   // If the key already exists, operator[]
   // changes the value of the datum in the element
   m1[ 2 ] = 40;

   // operator[] will also insert the value of the data
   // type's default constructor if the value is unspecified
   m1[5];

   cout  << "The keys of the mapped elements are now:";
   for ( pIter = m1.begin( ) ; pIter != m1.end( ) ; pIter++ )
      cout << " " << pIter -> first;
   cout << "." << endl;

   cout  << "The values of the mapped elements are now:";
   for ( pIter = m1.begin( ) ; pIter != m1.end( ) ; pIter++ )
      cout << " " << pIter -> second;
   cout << "." << endl;

// insert by moving key
    map<string, int> c2;
    string str("abc");
    cout << "c2[move(str)] == " << c2[move(str)] << endl;
    cout << "c2["abc"] == " << c2["abc"] << endl;

    return (0);
}
The keys of the mapped elements are: 1 2 3.
The values of the mapped elements are: 10 20 30.
The keys of the mapped elements are now: 1 2 3 5.
The values of the mapped elements are now: 10 40 30 0.
c2[move(str)] == 0
c2["abc"] == 1

operator=

別の map のコピーで map の要素を置き換えます。

map& operator=(const map& right);
map& operator=(map&& right);

パラメーター

right
map 内にコピーされる map

解説

map 内の既存の要素を消去した後、operator=right の内容を map 内にコピーまたは移動します。

// map_operator_as.cpp
// compile with: /EHsc
#include <map>
#include <iostream>

int main( )
   {
   using namespace std;
   map<int, int> v1, v2, v3;
   map<int, int>::iterator iter;

   v1.insert(pair<int, int>(1, 10));

   cout << "v1 = " ;
   for (iter = v1.begin(); iter != v1.end(); iter++)
      cout << iter->second << " ";
   cout << endl;

   v2 = v1;
   cout << "v2 = ";
   for (iter = v2.begin(); iter != v2.end(); iter++)
      cout << iter->second << " ";
   cout << endl;

// move v1 into v2
   v2.clear();
   v2 = move(v1);
   cout << "v2 = ";
   for (iter = v2.begin(); iter != v2.end(); iter++)
      cout << iter->second << " ";
   cout << endl;
   }

pointer

map 内の要素へのポインターを提供する型。

typedef typename allocator_type::pointer pointer;

解説

pointer は要素の値の変更に使用できます。

ほとんどの場合、map オブジェクト内の要素にアクセスするには、iterator を使用する必要があります。

rbegin

反転された map 内の最初の要素を指す反復子を返します。

const_reverse_iterator rbegin() const;

reverse_iterator rbegin();

戻り値

反転された map 内の最初の要素を指す、または反転されていない map 内の最後の要素だったものを指す、逆順双方向反復子。

解説

rbegin は、begin が map と一緒に使用されるのと同様に、反転された map で使用されます。

rbegin の戻り値が const_reverse_iterator に割り当てられている場合、map オブジェクトを変更できません。 rbegin の戻り値が reverse_iterator に割り当てられている場合、map オブジェクトを変更できます。

rbegin を使用して、map 内を後方に向かって反復処理できます。

// map_rbegin.cpp
// compile with: /EHsc
#include <map>
#include <iostream>

int main( )
{
   using namespace std;
   map <int, int> m1;

   map <int, int> :: iterator m1_Iter;
   map <int, int> :: reverse_iterator m1_rIter;
   map <int, int> :: const_reverse_iterator m1_crIter;
   typedef pair <int, int> Int_Pair;

   m1.insert ( Int_Pair ( 1, 10 ) );
   m1.insert ( Int_Pair ( 2, 20 ) );
   m1.insert ( Int_Pair ( 3, 30 ) );

   m1_rIter = m1.rbegin( );
   cout << "The first element of the reversed map m1 is "
        << m1_rIter -> first << "." << endl;

   // begin can be used to start an iteration
   // through a map in a forward order
   cout << "The map is: ";
   for ( m1_Iter = m1.begin( ) ; m1_Iter != m1.end( ); m1_Iter++)
      cout << m1_Iter -> first << " ";
      cout << "." << endl;

   // rbegin can be used to start an iteration
   // through a map in a reverse order
   cout << "The reversed map is: ";
   for ( m1_rIter = m1.rbegin( ) ; m1_rIter != m1.rend( ); m1_rIter++)
      cout << m1_rIter -> first << " ";
      cout << "." << endl;

   // A map element can be erased by dereferencing to its key
   m1_rIter = m1.rbegin( );
   m1.erase ( m1_rIter -> first );

   m1_rIter = m1.rbegin( );
   cout << "After the erasure, the first element "
        << "in the reversed map is "
        << m1_rIter -> first << "." << endl;
}
The first element of the reversed map m1 is 3.
The map is: 1 2 3 .
The reversed map is: 3 2 1 .
After the erasure, the first element in the reversed map is 2.

reference

map に格納されている要素への参照を提供する型。

typedef typename allocator_type::reference reference;

// map_reference.cpp
// compile with: /EHsc
#include <map>
#include <iostream>

int main( )
{
   using namespace std;
   map <int, int> m1;
   typedef pair <int, int> Int_Pair;

   m1.insert ( Int_Pair ( 1, 10 ) );
   m1.insert ( Int_Pair ( 2, 20 ) );

   // Declare and initialize a const_reference &Ref1
   // to the key of the first element
   const int &Ref1 = ( m1.begin( ) -> first );

   // The following line would cause an error because the
   // non-const_reference can't be used to access the key
   // int &Ref1 = ( m1.begin( ) -> first );

   cout << "The key of first element in the map is "
        << Ref1 << "." << endl;

   // Declare and initialize a reference &Ref2
   // to the data value of the first element
   int &Ref2 = ( m1.begin( ) -> second );

   cout << "The data value of first element in the map is "
        << Ref2 << "." << endl;

   //The non-const_reference can be used to modify the
   //data value of the first element
   Ref2 = Ref2 + 5;
   cout << "The modified data value of first element is "
        << Ref2 << "." << endl;
}
The key of first element in the map is 1.
The data value of first element in the map is 10.
The modified data value of first element is 15.

rend

反転された map 内の最後の要素の次の位置を指す反復子を返します。

const_reverse_iterator rend() const;

reverse_iterator rend();

戻り値

反転された map 内の最後の要素の次の場所 (反転されていない map 内の最初の要素の前の場所) を指す、逆順双方向反復子。

解説

rend は、end が map と一緒に使用されるのと同様に、反転された map で使用されます。

rend の戻り値が const_reverse_iterator に割り当てられている場合、map オブジェクトを変更できません。 rend の戻り値が reverse_iterator に割り当てられている場合、map オブジェクトを変更できます。

rend を使用して、逆順反復子が map の末尾に達したかどうかをテストできます。

rend によって返された値は逆参照しないでください。

// map_rend.cpp
// compile with: /EHsc
#include <map>
#include <iostream>

int main( )
{
   using namespace std;
   map <int, int> m1;

   map <int, int> :: iterator m1_Iter;
   map <int, int> :: reverse_iterator m1_rIter;
   map <int, int> :: const_reverse_iterator m1_crIter;
   typedef pair <int, int> Int_Pair;

   m1.insert ( Int_Pair ( 1, 10 ) );
   m1.insert ( Int_Pair ( 2, 20 ) );
   m1.insert ( Int_Pair ( 3, 30 ) );

   m1_rIter = m1.rend( );
   m1_rIter--;
   cout << "The last element of the reversed map m1 is "
        << m1_rIter -> first << "." << endl;

   // begin can be used to start an iteration
   // through a map in a forward order
   cout << "The map is: ";
   for ( m1_Iter = m1.begin( ) ; m1_Iter != m1.end( ); m1_Iter++)
      cout << m1_Iter -> first << " ";
      cout << "." << endl;

   // rbegin can be used to start an iteration
   // through a map in a reverse order
   cout << "The reversed map is: ";
   for ( m1_rIter = m1.rbegin( ) ; m1_rIter != m1.rend( ); m1_rIter++)
      cout << m1_rIter -> first << " ";
      cout << "." << endl;

   // A map element can be erased by dereferencing to its key
   m1_rIter = --m1.rend( );
   m1.erase ( m1_rIter -> first );

   m1_rIter = m1.rend( );
   m1_rIter--;
   cout << "After the erasure, the last element "
        << "in the reversed map is "
        << m1_rIter -> first << "." << endl;
}
The last element of the reversed map m1 is 1.
The map is: 1 2 3 .
The reversed map is: 3 2 1 .
After the erasure, the last element in the reversed map is 2.

reverse_iterator

反転された map 内の 1 つの要素の読み取りまたは変更ができる双方向反復子を提供する型。

typedef std::reverse_iterator<iterator> reverse_iterator;

解説

reverse_iterator 型は要素の値を変更できず、逆の順序で map を反復処理するために使用します。

map によって定義される reverse_iterator は、value_type のオブジェクトである要素を指します。これは pair<const Key, Type> 型で、その最初のメンバーは要素へのキーであり、2 番目のメンバーは要素が保持するマップされたデータです。

マップ内の要素を指す reverse_iteratorrIter を逆参照するには、-> 演算子を使用します。

要素のキーの値にアクセスするには rIter ->first を使用しますが、これは (* rIter). first と同等です。 要素のマップされたデータの値にアクセスするには rIter ->second を使用しますが、これは (* rIter). first と同等です。

reverse_iterator の宣言方法や使用方法の例については、rbegin の例を参照してください。

size

map 内の要素数を返します。

size_type size() const;

戻り値

map の現在の長さ。

map::size メンバー関数の使用例を次に示します。

// map_size.cpp
// compile with: /EHsc
#include <map>
#include <iostream>

int main()
{
    using namespace std;
    map<int, int> m1, m2;
    map<int, int>::size_type i;
    typedef pair<int, int> Int_Pair;

    m1.insert(Int_Pair(1, 1));
    i = m1.size();
    cout << "The map length is " << i << "." << endl;

    m1.insert(Int_Pair(2, 4));
    i = m1.size();
    cout << "The map length is now " << i << "." << endl;
}
The map length is 1.
The map length is now 2.

size_type

map 内の要素の数を表すことができる符号なし整数型。

typedef typename allocator_type::size_type size_type;

size_type の宣言や使用の方法の例については、size の例を参照してください。

スワップ

2 つの map の要素を交換します。

void swap(
    map<Key, Type, Traits, Allocator>& right);

パラメーター

right
ターゲットの map と交換する要素を提供する引数の map。

解説

メンバー関数は、要素を交換する 2 つの map において要素を指定している参照、ポインター、反復子を無効化することはありません。

// map_swap.cpp
// compile with: /EHsc
#include <map>
#include <iostream>

int main( )
{
   using namespace std;
   map <int, int> m1, m2, m3;
   map <int, int>::iterator m1_Iter;
   typedef pair <int, int> Int_Pair;

   m1.insert ( Int_Pair ( 1, 10 ) );
   m1.insert ( Int_Pair ( 2, 20 ) );
   m1.insert ( Int_Pair ( 3, 30 ) );
   m2.insert ( Int_Pair ( 10, 100 ) );
   m2.insert ( Int_Pair ( 20, 200 ) );
   m3.insert ( Int_Pair ( 30, 300 ) );

   cout << "The original map m1 is:";
   for ( m1_Iter = m1.begin( ); m1_Iter != m1.end( ); m1_Iter++ )
      cout << " " << m1_Iter -> second;
   cout   << "." << endl;

   // This is the member function version of swap
   //m2 is said to be the argument map; m1 the target map
   m1.swap( m2 );

   cout << "After swapping with m2, map m1 is:";
   for ( m1_Iter = m1.begin( ); m1_Iter != m1.end( ); m1_Iter++ )
      cout << " " << m1_Iter -> second;
   cout  << "." << endl;

   // This is the specialized template version of swap
   swap( m1, m3 );

   cout << "After swapping with m3, map m1 is:";
   for ( m1_Iter = m1.begin( ); m1_Iter != m1.end( ); m1_Iter++ )
      cout << " " << m1_Iter -> second;
   cout   << "." << endl;
}
The original map m1 is: 10 20 30.
After swapping with m2, map m1 is: 100 200.
After swapping with m3, map m1 is: 300.

upper_bound

指定したキーよりも大きいキー値を持つ map 内の最初の要素を指す反復子を返します。

iterator upper_bound(const Key& key);

const_iterator upper_bound(const Key& key) const;

パラメーター

key
検索対象の map 内の要素の並べ替えキー値と比較される引数キー値。

戻り値

引数キーより大きいキーを持つ map 内の要素の位置を指す、または、キーの一致が検出されない場合は map 内の最後の要素の次の位置を指す、iterator または const_iterator

戻り値が const_iterator に割り当てられている場合、map オブジェクトは変更できません。 戻り値が iterator に割り当てられている場合、map オブジェクトは変更できます。

// map_upper_bound.cpp
// compile with: /EHsc
#include <map>
#include <iostream>

int main( )
{
   using namespace std;
   map <int, int> m1;
   map <int, int> :: const_iterator m1_AcIter, m1_RcIter;
   typedef pair <int, int> Int_Pair;

   m1.insert ( Int_Pair ( 1, 10 ) );
   m1.insert ( Int_Pair ( 2, 20 ) );
   m1.insert ( Int_Pair ( 3, 30 ) );

   m1_RcIter = m1.upper_bound( 2 );
   cout << "The first element of map m1 with a key "
        << "greater than 2 is: "
        << m1_RcIter -> second << "." << endl;

   // If no match is found for the key, end is returned
   m1_RcIter = m1. upper_bound ( 4 );

   if ( m1_RcIter == m1.end( ) )
      cout << "The map m1 doesn't have an element "
           << "with a key greater than 4." << endl;
   else
      cout << "The element of map m1 with a key > 4 is: "
           << m1_RcIter -> second << "." << endl;

   // The element at a specific location in the map can be found
   // using a dereferenced iterator addressing the location
   m1_AcIter = m1.begin( );
   m1_RcIter = m1. upper_bound ( m1_AcIter -> first );
   cout << "The 1st element of m1 with a key greater than\n"
        << "that of the initial element of m1 is: "
        << m1_RcIter -> second << "." << endl;
}
The first element of map m1 with a key greater than 2 is: 30.
The map m1 doesn't have an element with a key greater than 4.
The 1st element of m1 with a key greater than
that of the initial element of m1 is: 20.

value_comp

このメンバー関数は、キー値の比較によって map の要素の順序を決定する関数オブジェクトを返します。

value_compare value_comp() const;

戻り値

map が要素の並べ替えに使用する比較関数オブジェクトを返します。

解説

map m の場合、2 つの要素 e1(k1, d1) と e2(k2, d2) が value_type 型のオブジェクトである場合 (ここで、k1k1 は型 key_typed1 のキーであり、d2 は型 mapped_type のデータ)、m.value_comp(e1, e2)m.key_comp(k1, k2) と同等です。 格納されているオブジェクトは以下のメンバー関数を定義します。

bool operator( value_type& left, value_type& right);

これは、並べ替え順で left のキー値が right のキー値に先行しかつ等しくない場合に、true を返します。

// map_value_comp.cpp
// compile with: /EHsc
#include <map>
#include <iostream>

int main( )
{
   using namespace std;

   map <int, int, less<int> > m1;
   map <int, int, less<int> >::value_compare vc1 = m1.value_comp( );
   pair< map<int,int>::iterator, bool > pr1, pr2;

   pr1= m1.insert ( map <int, int> :: value_type ( 1, 10 ) );
   pr2= m1.insert ( map <int, int> :: value_type ( 2, 5 ) );

   if( vc1( *pr1.first, *pr2.first ) == true )
   {
      cout << "The element ( 1,10 ) precedes the element ( 2,5 )."
           << endl;
   }
   else
   {
      cout << "The element ( 1,10 ) does not precede the element ( 2,5 )."
           << endl;
   }

   if(vc1( *pr2.first, *pr1.first ) == true )
   {
      cout << "The element ( 2,5 ) precedes the element ( 1,10 )."
           << endl;
   }
   else
   {
      cout << "The element ( 2,5 ) does not precede the element ( 1,10 )."
           << endl;
   }
}
The element ( 1,10 ) precedes the element ( 2,5 ).
The element ( 2,5 ) does not precede the element ( 1,10 ).

value_type

map 内に要素として格納されるオブジェクトの種類。

typedef pair<const Key, Type> value_type;

// map_value_type.cpp
// compile with: /EHsc
#include <map>
#include <iostream>

int main( )
{
   using namespace std;
   typedef pair <const int, int> cInt2Int;
   map <int, int> m1;
   map <int, int> :: key_type key1;
   map <int, int> :: mapped_type mapped1;
   map <int, int> :: value_type value1;
   map <int, int> :: iterator pIter;

   // value_type can be used to pass the correct type
   // explicitly to avoid implicit type conversion
   m1.insert ( map <int, int> :: value_type ( 1, 10 ) );

   // Compare other ways to insert objects into a map
   m1.insert ( cInt2Int ( 2, 20 ) );
   m1[ 3 ] = 30;

   // Initializing key1 and mapped1
   key1 = ( m1.begin( ) -> first );
   mapped1 = ( m1.begin( ) -> second );

   cout << "The key of first element in the map is "
        << key1 << "." << endl;

   cout << "The data value of first element in the map is "
        << mapped1 << "." << endl;

   // The following line would cause an error because
   // the value_type isn't assignable
   // value1 = cInt2Int ( 4, 40 );

   cout  << "The keys of the mapped elements are:";
   for ( pIter = m1.begin( ) ; pIter != m1.end( ) ; pIter++ )
      cout << " " << pIter -> first;
   cout << "." << endl;

   cout  << "The values of the mapped elements are:";
   for ( pIter = m1.begin( ) ; pIter != m1.end( ) ; pIter++ )
      cout << " " << pIter -> second;
   cout << "." << endl;
}

関連項目

Containers
C++ 標準ライブラリ内のスレッド セーフ
C++ 標準ライブラリ リファレンス