std::unordered_map<Key,T,Hash,KeyEqual,Allocator>:: operator[]
|
T
&
operator
[
]
(
const
Key
&
key
)
;
|
(1) | (C++11以降) |
|
T
&
operator
[
]
(
Key
&&
key
)
;
|
(2) | (C++11以降) |
|
template
<
class
K
>
T & operator [ ] ( K && x ) ; |
(3) | (C++26以降) |
キーが key または x と等価である値への参照を返します。該当するキーが存在しない場合は挿入を実行します。
value_type
オブジェクトを
std::
piecewise_construct
,
std::
forward_as_tuple
(
key
)
,
std::
tuple
<>
(
)
からその場で構築して挿入します。
key
からコピー構築され、マップされた値が
値初期化
されることを意味する。
-
value_type
は
EmplaceConstructible
でなければならない(
std::
piecewise_construct
、
std::
forward_as_tuple
(
key
)
、
std::
tuple
<>
(
)
から構築可能)。デフォルトのアロケータを使用する場合、これは
key_type
が
CopyConstructible
であり、
mapped_type
が
DefaultConstructible
でなければならないことを意味する。
|
value_type
オブジェクトを
std::
piecewise_construct
,
std::
forward_as_tuple
(
std
::
move
(
key
)
)
,
std::
tuple
<>
(
)
からその場で構築して挿入します。
デフォルトアロケータを使用する場合、これはキーが
key
からムーブ構築され、マップされた値が
値初期化
されることを意味します。
-
value_type
は
EmplaceConstructible
でなければならず、
std::
piecewise_construct
,
std::
forward_as_tuple
(
std
::
move
(
key
)
)
,
std::
tuple
<>
(
)
から構築可能であること。デフォルトアロケータを使用する場合、これは
key_type
が
MoveConstructible
であり、
mapped_type
が
DefaultConstructible
でなければならないことを意味します。
|
Hash
と
KeyEqual
の両方が
透過的
である場合にのみ、オーバーロード解決に参加します。これは、そのような
Hash
が
K
型と
Key
型の両方で呼び出し可能であり、かつ
KeyEqual
が透過的であることを前提としています。これにより、
Key
のインスタンスを構築せずにこの関数を呼び出すことが可能になります。
操作後に新しい要素数が元の
max_load_factor()
*
bucket_count()
より大きい場合、再ハッシュが行われます。
再ハッシュが発生した場合(挿入による)、すべてのイテレータは無効化されます。それ以外の場合(再ハッシュなし)、イテレータは無効化されません。
目次 |
パラメータ
| key | - | 検索する要素のキー |
| x | - | キーと透過的に比較可能な任意の型の値 |
戻り値
例外
いずれかの操作で例外がスローされた場合、挿入は無効となります。
計算量
平均ケース: 定数時間、最悪ケース: サイズに対して線形時間。
注記
公開されたC++11およびC++14規格では、この関数は
mapped_type
が
DefaultInsertable
であり、
key_type
が
CopyInsertable
または
MoveInsertable
であることを要求するように規定されていました。この規定には欠陥があり、
LWG issue 2469
によって修正されました。上記の説明はこの問題の解決策を組み込んだものです。
しかし、1つの実装(libc++)は、公開された標準でおそらく要求されている通り、
key_type
および
mapped_type
オブジェクトを2つの別々のアロケータ
construct()
呼び出しで構築することが知られており、
value_type
オブジェクトをエンプレースするのではありません。
operator
[
]
は、キーが存在しない場合に挿入を行うため非constです。この動作が望ましくない場合、またはコンテナが
const
である場合は、
at
を使用できます。
|
|
(C++17以降) |
| 機能テスト マクロ | 値 | 標準 | 機能 |
|---|---|---|---|
__cpp_lib_associative_heterogeneous_insertion
|
202311L
|
(C++26) | 順序付き および 非順序 連想 コンテナ の残りのメンバー関数に対する異種オーバーロード ( 3 ) |
例
#include <iostream> #include <string> #include <unordered_map> void println(auto const comment, auto const& map) { std::cout << comment << '{'; for (const auto& pair : map) std::cout << '{' << pair.first << ": " << pair.second << '}'; std::cout << "}\n"; } int main() { std::unordered_map<char, int> letter_counts{{'a', 27}, {'b', 3}, {'c', 1}}; println("letter_counts initially contains: ", letter_counts); letter_counts['b'] = 42; // 既存の値を更新 letter_counts['x'] = 9; // 新しい値を挿入 println("after modifications it contains: ", letter_counts); // 各単語の出現回数をカウント // (operator[]の最初の呼び出しでカウンタをゼロで初期化) std::unordered_map<std::string, int> word_map; for (const auto& w : {"this", "sentence", "is", "not", "a", "sentence", "this", "sentence", "is", "a", "hoax"}) ++word_map[w]; word_map["that"]; // 単にペア {"that", 0} を挿入 for (const auto& [word, count] : word_map) std::cout << count << " occurrence(s) of word '" << word << "'\n"; }
出力例:
letter_counts initially contains: {{a: 27}{b: 3}{c: 1}}
after modifications it contains: {{a: 27}{b: 42}{c: 1}{x: 9}}
2 occurrence(s) of word 'a'
1 occurrence(s) of word 'hoax'
2 occurrence(s) of word 'is'
1 occurrence(s) of word 'not'
3 occurrence(s) of word 'sentence'
0 occurrence(s) of word 'that'
2 occurrence(s) of word 'this'
関連項目
|
境界チェック付きで指定された要素にアクセスする
(公開メンバ関数) |
|
|
(C++17)
|
要素を挿入するか、キーが既に存在する場合は現在の要素に代入する
(公開メンバ関数) |
|
(C++17)
|
キーが存在しない場合はその場で挿入し、キーが存在する場合は何もしない
(公開メンバ関数) |