map::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;
}
出力
map starting data: 3 elements:
(Anna,Accounting) (Bob,Accounting) (Carmine,Engineering)
map modified, now contains 4 elements:
(Anna,Accounting) (Bob,Accounting) (Carmine,Engineering) (Doug,Engineering)
必要条件
ヘッダー: <map>
名前空間: std