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以降) |
目次パラメータ
計算量
1-5)
定数。
9,10)
サイズに対して線形
other
.
11,12)
定数。もし
alloc
が与えられ、かつ
alloc
!
=
other.
get_allocator
(
)
の場合、線形。
例外
注釈コンテナのムーブ構築(オーバーロード (4))の後、元々 C++23まで正式に要求されていないが、一部の実装では既により古いモードでテンプレートパラメータ
例
このコードを実行
#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++規格に対して遡及的に適用されました。
関連項目
| ||||||||||||||||||||||||||||||||||||||||||||||||||||