std::map<Key,T,Compare,Allocator>:: insert
|
std::
pair
<
iterator,
bool
>
insert
(
const
value_type
&
value
)
;
|
(1) | |
|
template
<
class
P
>
std:: pair < iterator, bool > insert ( P && value ) ; |
(2) | (C++11以降) |
|
std::
pair
<
iterator,
bool
>
insert
(
value_type
&&
value
)
;
|
(3) | (C++17以降) |
| (4) | ||
|
iterator insert
(
iterator pos,
const
value_type
&
value
)
;
|
(C++11以前) | |
|
iterator insert
(
const_iterator pos,
const
value_type
&
value
)
;
|
(C++11以降) | |
|
template
<
class
P
>
iterator insert ( const_iterator pos, P && value ) ; |
(5) | (C++11以降) |
|
iterator insert
(
const_iterator pos, value_type
&&
value
)
;
|
(6) | (C++17以降) |
|
template
<
class
InputIt
>
void insert ( InputIt first, InputIt last ) ; |
(7) | |
|
void
insert
(
std::
initializer_list
<
value_type
>
ilist
)
;
|
(8) | (C++11以降) |
|
insert_return_type insert
(
node_type
&&
nh
)
;
|
(9) | (C++17以降) |
|
iterator insert
(
const_iterator pos, node_type
&&
nh
)
;
|
(10) | (C++17以降) |
コンテナに要素を挿入します。ただし、同等のキーを持つ要素が既にコンテナに含まれていない場合に限ります。
イテレータおよび参照は無効化されません。 挿入が成功した場合、ノードハンドル内に保持されている間に取得された要素へのポインタおよび参照は無効化され、抽出前にその要素に対して取得されたポインタおよび参照は有効になります。 (C++17以降)
目次 |
パラメータ
| pos | - | 新しい要素が挿入される位置の前を指すイテレータ |
| value | - | 挿入する要素の値 |
| first, last | - | 挿入する要素の 範囲 を定義するイテレータのペア |
| ilist | - | 値の挿入元となる初期化子リスト |
| nh | - | 互換性のある ノードハンドル |
| 型要件 | ||
-
InputIt
は
LegacyInputIterator
の要件を満たさなければならない。
|
||
戻り値
insert_return_type
のオブジェクトが以下のように初期化されます:
-
nh
が空の場合、
insertedは false 、positionは end ( ) 、nodeは空となります。 -
挿入が行われた場合、
insertedは true 、positionは挿入された要素を指し、nodeは空となります。 -
挿入が失敗した場合、
insertedは false 、nodeは nh の前の値を持ち、positionは nh. key ( ) と等価なキーを持つ要素を指します。
例外
|
このセクションは不完全です
理由: ケース 7-10 |
計算量
O(log(size()))
。
O(N·log(size() + N))
、ここで
N
は挿入する要素の数です。
O(log(size()))
。
注記
ヒント付き挿入
(
(
4-6
)
および
(
10
)
)は、
std::vector::insert
のようなシーケンシャルコンテナでの位置指定挿入とのシグネチャ互換性を保つため、ブール値を返しません。これにより、
std::inserter
のような汎用インサーターを作成することが可能になります。ヒント付き挿入の成功を確認する一つの方法は、挿入前後の
size()
を比較することです。
例
#include <iomanip> #include <iostream> #include <map> #include <string> using namespace std::リテラル; template<typename It> void print_insertion_status(It it, bool success) { std::cout << "の挿入" << it->first << (success ? " 成功しました\n" : " 失敗しました\n"); } int main() { std::map<std::string, float> heights; // オーバーロード 3: rvalue 参照からの挿入 const auto [it_hinata, success] = heights.insert({"日向"s, 162.8}); print_insertion_status(it_hinata, success); { // オーバーロード 1: lvalue 参照からの挿入 const auto [it, success2] = heights.insert(*it_hinata); print_insertion_status(it, success2); } { // オーバーロード 2: emplaceへの転送による挿入 const auto [it, success] = heights.insert(std::pair{"影山", 180.6}); print_insertion_status(it, success); } { // オーバーロード 6: 位置ヒント付き右辺値参照からの挿入 const std::size_t n = std::size(heights); const auto it = heights.insert(it_hinata, {"Azumane"s, 184.7}); print_insertion_status(it, std::size(heights) != n); } { // オーバーロード 4: 位置ヒント付きで左辺値参照から挿入 const std::size_t n = std::size(heights); const auto it = heights.insert(it_hinata, *it_hinata); print_insertion_status(it, std::size(heights) != n); } { // オーバーロード 5: 位置ヒント付き emplace への転送による挿入 const std::size_t n = std::size(heights); const auto it = heights.insert(it_hinata, std::pair{"月島", 188.3}); print_insertion_status(it, std::size(heights) != n); } auto node_hinata = heights.extract(it_hinata); std::map<std::string, float> heights2; // オーバーロード 7: イテレータ範囲からの挿入 heights2.insert(std::begin(heights), std::end(heights)); // オーバーロード 8: initializer_list からの挿入 heights2.insert({{"Kozume"s, 169.2}, {"黒尾", 187.7}}); // オーバーロード 9: ノードの挿入 const auto status = heights2.insert(std::move(node_hinata)); print_insertion_status(status.position, status.inserted); node_hinata = heights2.extract(status.position); { // オーバーロード 10: 位置ヒント付きでノードを挿入 const std::size_t n = std::size(heights2); const auto it = heights2.insert(std::begin(heights2), std::move(node_hinata)); print_insertion_status(it, std::size(heights2) != n); } // 結果のマップを出力 std::cout << std::left << '\n'; for (const auto& [name, height] : heights2) std::cout << std::setw(10) << name << " | " << height << "cm\n"; }
出力:
日向の挿入に成功しました 日向の挿入に失敗しました 影山の挿入に成功しました 東峰の挿入に成功しました 日向の挿入に失敗しました 月島の挿入に成功しました 日向の挿入に成功しました 日向の挿入に成功しました Azumane | 184.7cm Hinata | 162.8cm Kageyama | 180.6cm Kozume | 169.2cm Kuroo | 187.7cm Tsukishima | 188.3cm
欠陥報告
以下の動作変更の欠陥報告書は、以前に公開されたC++規格に対して遡及的に適用されました。
| DR | 適用対象 | 公開時の動作 | 正しい動作 |
|---|---|---|---|
| LWG 233 | C++98 | pos は単なるヒントであり、完全に無視される可能性があった |
挿入は
pos
の直前の位置に
可能な限り近くなるように 行われることが要求される |
| LWG 264 | C++98 |
オーバーロード
(
7
)
の計算量は、範囲
[
first
,
last
)
が
Compare
に従って
ソートされている場合、線形であることが要求されていた |
この特別なケースにおける
線形計算量の要件を削除 |
| LWG 316 | C++98 |
オーバーロード
(
1
)
の戻り値において、どの
bool
値が
挿入の成功を示すかが規定されていなかった |
成功は
true
によって
示される |
| LWG 2005 | C++11 | オーバーロード ( 2 ) と ( 5 ) の説明が不十分であった | 説明を改善 |
関連項目
|
(C++11)
|
要素をその場で構築する
(公開メンバ関数) |
|
(C++11)
|
ヒントを使用して要素をその場で構築する
(公開メンバ関数) |
|
(C++17)
|
要素を挿入するか、キーが既に存在する場合は現在の要素に代入する
(公開メンバ関数) |
|
引数から推論された型の
std::insert_iterator
を作成する
(関数テンプレート) |