unordered_multimap::insert
更新 : 2007 年 11 月
要素を追加します。
iterator insert(const value_type& val);
iterator insert(iterator where, const value_type& val);
template<class InIt>
void insert(InIt first, InIt last);
パラメータ
InIt
反復子の型。first
挿入する範囲の先頭。last
挿入する範囲の最後。val
挿入する値。where
コンテナ内の挿入位置 (ヒントのみ)。
解説
1 つ目のメンバ関数は、被制御シーケンスに要素 val を挿入した後、挿入された要素を指定する反復子を返します。2 つ目のメンバ関数は、被制御シーケンス内の挿入位置の検索開始位置に where を使用して、insert(val) を返します。挿入位置が where の直前または直後である場合、挿入処理が若干速くなる可能性があります。3 つ目のメンバ関数は、範囲 [first, last) 内の各 where について、insert(*where) を呼び出すことによって、要素値のシーケンスを挿入します。
単一要素の挿入時に例外がスローされた場合、コンテナは変更されず、再度例外がスローされます。複数要素の挿入時に例外がスローされた場合、コンテナは安定かつ未指定の状態になり、再度例外がスローされます。
使用例
// std_tr1__unordered_map__unordered_multimap_insert.cpp
// compile with: /EHsc
#include <unordered_map>
#include <iostream>
typedef std::tr1::unordered_multimap<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));
// display contents " [c 3] [b 2] [a 1]"
for (Mymap::const_iterator it = c1.begin();
it != c1.end(); ++it)
std::cout << " [" << it->first << ", " << it->second << "]";
std::cout << std::endl;
// insert with hint and reinspect
Mymap::iterator it2 = c1.insert(c1.begin(), Mymap::value_type('d', 4));
for (Mymap::const_iterator it = c1.begin();
it != c1.end(); ++it)
std::cout << " [" << it->first << ", " << it->second << "]";
std::cout << std::endl;
// insert range and inspect
Mymap c2;
c2.insert(c1.begin(), c1.end());
for (Mymap::const_iterator it = c2.begin();
it != c2.end(); ++it)
std::cout << " [" << it->first << ", " << it->second << "]";
std::cout << std::endl;
// insert new and duplicate, and reinspect
c1.insert(Mymap::value_type('e', 5));
c1.insert(Mymap::value_type('a', 6));
for (Mymap::const_iterator it = c1.begin();
it != c1.end(); ++it)
std::cout << " [" << it->first << ", " << it->second << "]";
std::cout << std::endl;
return (0);
}
[c, 3] [b, 2] [a, 1]
[d, 4] [c, 3] [b, 2] [a, 1]
[d, 4] [c, 3] [b, 2] [a, 1]
[e, 5] [d, 4] [c, 3] [b, 2] [a, 1] [a, 6]
必要条件
ヘッダー : <unordered_map>
名前空間 : std::tr1