std::unordered_map<Key,T,Hash,KeyEqual,Allocator>:: unordered_map
| (1) | ||
|
unordered_map
(
)
: unordered_map ( size_type ( /* unspecified */ ) ) { } |
(C++11以降)
(C++20まで) |
|
|
unordered_map
(
)
;
|
(C++20以降) | |
|
explicit
unordered_map
(
size_type bucket_count,
const
Hash
&
hash
=
Hash
(
)
,
|
(2) | (C++11以降) |
|
unordered_map
(
size_type bucket_count,
const
Allocator
&
alloc
)
|
(3) | (C++14以降) |
|
unordered_map
(
size_type bucket_count,
const
Hash
&
hash,
|
(4) | (C++14以降) |
|
explicit
unordered_map
(
const
Allocator
&
alloc
)
;
|
(5) | (C++11以降) |
|
template
<
class
InputIt
>
unordered_map
(
InputIt first, InputIt last,
|
(6) | (C++11以降) |
|
template
<
class
InputIt
>
unordered_map
(
InputIt first, InputIt last,
|
(7) | (C++14以降) |
|
template
<
class
InputIt
>
unordered_map
(
InputIt first, InputIt last,
|
(8) | (C++14以降) |
|
unordered_map
(
const
unordered_map
&
other
)
;
|
(9) | (C++11以降) |
|
unordered_map
(
const
unordered_map
&
other,
const
Allocator
&
alloc
)
;
|
(10) | (C++11以降) |
|
unordered_map
(
unordered_map
&&
other
)
;
|
(11) | (C++11以降) |
|
unordered_map
(
unordered_map
&&
other,
const
Allocator
&
alloc
)
;
|
(12) | (C++11以降) |
|
unordered_map
(
std::
initializer_list
<
value_type
>
init,
size_type bucket_count
=
/* 未指定 */
,
|
(13) | (C++11以降) |
|
unordered_map
(
std::
initializer_list
<
value_type
>
init,
size_type bucket_count,
|
(14) | (C++14以降) |
|
unordered_map
(
std::
initializer_list
<
value_type
>
init,
size_type bucket_count,
|
(15) | (C++14以降) |
|
template
<
container-compatible-range
<
value_type
>
R
>
unordered_map
(
std::
from_range_t
, R
&&
rg,
|
(16) | (C++23以降) |
|
template
<
container-compatible-range
<
value_type
>
R
>
unordered_map
(
std::
from_range_t
, R
&&
rg,
|
(17) | (C++23以降) |
|
template
<
container-compatible-range
<
value_type
>
R
>
unordered_map
(
std::
from_range_t
, R
&&
rg,
|
(18) | (C++23以降) |
様々なデータソースから新しいコンテナを構築します。オプションでユーザー指定の bucket_count を最小バケット数として使用し、 hash をハッシュ関数、 equal をキー比較関数、 alloc をアロケータとして使用します。
[
first
,
last
)
の内容でコンテナを構築します。
max_load_factor()
を
1.0
に設定します。範囲内の複数の要素が同等のキーを持つ場合、どの要素が挿入されるかは未規定です(
LWG2844
保留中)。
|
テンプレートパラメータ
|
(C++23以降) |
|
テンプレートパラメータ
|
(C++23以降) |
目次 |
パラメータ
| alloc | - | このコンテナのすべてのメモリ割り当てに使用するアロケータ |
| bucket_count | - | 初期化時に使用する最小バケット数。指定されない場合、未規定のデフォルト値が使用される |
| hash | - | 使用するハッシュ関数 |
| equal | - | このコンテナのすべてのキー比較に使用する比較関数 |
| first, last | - | コピーする要素のソース 範囲 を定義するイテレータのペア |
| rg | - |
コンテナ互換範囲
、すなわち要素が
value_type
に変換可能な
input_range
|
| other | - | コンテナの要素を初期化するためのソースとして使用する別のコンテナ |
| init | - | コンテナの要素を初期化するための初期化子リスト |
| 型要件 | ||
-
InputIt
は
LegacyInputIterator
の要件を満たさなければならない
|
||
計算量
例外
Allocator::allocate
への呼び出しは例外をスローする可能性があります。
注記
C++23で正式に要求される前から、一部の実装では既にテンプレートパラメータ
Allocator
を
非推定コンテキスト
に置いていたものもあります。
| 機能テスト マクロ | 値 | 標準 | 機能 |
|---|---|---|---|
__cpp_lib_containers_ranges
|
202202L
|
(C++23) | Ranges-aware 構築と挿入; オーバーロード ( 16-18 ) |
例
#include <bitset> #include <string> #include <unordered_map> #include <utility> #include <vector> struct Key { std::string first; std::string second; }; struct KeyHash { std::size_t operator()(const Key& k) const { return std::hash<std::string>()(k.first) ^ (std::hash<std::string>()(k.second) << 1); } }; struct KeyEqual { bool operator()(const Key& lhs, const Key& rhs) const { return lhs.first == rhs.first && lhs.second == rhs.second; } }; struct Foo { Foo(int val_) : val(val_) {} int val; bool operator==(const Foo &rhs) const { return val == rhs.val; } }; template<> struct std::hash<Foo> { std::size_t operator()(const Foo &f) const { return std::hash<int>{}(f.val); } }; int main() { // デフォルトコンストラクタ: 空のマップ std::unordered_map<std::string, std::string> m1; // リストコンストラクタ std::unordered_map<int, std::string> m2 = { {1, "foo"}, {3, "bar"}, {2, "baz"} }; // コピーコンストラクタ std::unordered_map<int, std::string> m3 = m2; // move constructor std::unordered_map<int, std::string> m4 = std::move(m2); // レンジコンストラクタ std::vector<std::pair<std::bitset<8>, int>> v = {{0x12, 1}, {0x01,-1}}; std::unordered_map<std::bitset<8>, double> m5(v.begin(), v.end()); // カスタム Key 型を持つコンストラクタのオプション 1 // KeyHashおよびKeyEqual構造体を定義し、テンプレート内で使用する std::unordered_map<Key, std::string, KeyHash, KeyEqual> m6 = { {{"ジョン", "Doe"}, "example"}, {{"Mary", "スー"}, "another"} }; // カスタム Key 型を使用するコンストラクタのオプション 2。 // クラス/構造体に対してconst ==演算子を定義し、std::hashを特殊化する // std名前空間内の構造体 std::unordered_map<Foo, std::string> m7 = { {Foo(1), "One"}, {2, "二"}, {3, "スリー"} }; // オプション3: ラムダ式を使用 // 初期バケット数はコンストラクタに渡す必要があることに注意 struct Goo { int val; }; auto hash = [](const Goo &g){ return std::hash<int>{}(g.val); }; auto comp = [](const Goo &l, const Goo &r){ return l.val == r.val; }; std::unordered_map<Goo, double, decltype(hash), decltype(comp)> m8(10, hash, comp); }
欠陥報告
以下の動作変更の欠陥報告書は、以前に公開されたC++規格に対して遡及的に適用されました。
| DR | 適用対象 | 公開時の動作 | 正しい動作 |
|---|---|---|---|
| LWG 2193 | C++11 | デフォルトコンストラクタ ( 1 ) がexplicitであった | non-explicitに変更 |
| LWG 2230 | C++11 | オーバーロード ( 13 ) のセマンティクスが規定されていなかった | 規定された |
関連項目
|
コンテナに値を代入する
(公開メンバ関数) |