std::unordered_map<Key,T,Hash,KeyEqual,Allocator>::unordered_map
unordered_map(): unordered_map( size_type(/*implementation-defined*/)){} explicit unordered_map( size_type bucket_count, | (1) | (C++11以上) |
unordered_map( size_type bucket_count, const Allocator& alloc ) | (1) | (C++14以上) |
explicit unordered_map(const Allocator& alloc ); | (1) | (C++11以上) |
template<class InputIt > unordered_map( InputIt first, InputIt last, | (2) | (C++11以上) |
template<class InputIt > unordered_map( InputIt first, InputIt last, | (2) | (C++14以上) |
template<class InputIt > unordered_map( InputIt first, InputIt last, | (2) | (C++14以上) |
unordered_map(const unordered_map& other ); | (3) | (C++11以上) |
unordered_map(const unordered_map& other, const Allocator& alloc ); | (3) | (C++11以上) |
unordered_map( unordered_map&& other ); | (4) | (C++11以上) |
unordered_map( unordered_map&& other, const Allocator& alloc ); | (4) | (C++11以上) |
unordered_map(std::initializer_list<value_type> init, size_type bucket_count =/*implementation-defined*/, | (5) | (C++11以上) |
unordered_map(std::initializer_list<value_type> init, size_type bucket_count, | (5) | (C++14以上) |
unordered_map(std::initializer_list<value_type> init, size_type bucket_count, | (5) | (C++14以上) |
様々なデータソースから新しいコンテナを構築します。 オプションで作成する最小バケット数 bucket_count
、ハッシュ関数 hash
、キーを比較するための関数 equal
、アロケータ alloc
を指定できます。
max_load_factor()
は 1.0 に設定されます。 デフォルトコンストラクタの場合、バケット数は処理系定義です。[first, last)
の内容を持つコンテナを構築します。 max_load_factor()
は 1.0 に設定されます。 指定範囲内の複数の要素が等しいキーを持っている場合、どの要素が挿入されるかは未規定です (未解決の LWG2844)。other
の内容のコピーを持つコンテナを構築します。 負荷係数、述語、ハッシュ関数も同様にコピーされます。 alloc
が指定されない場合、アロケータは std::allocator_traits<allocator_type>::select_on_container_copy_construction(other.get_allocator()) を呼ぶことによって取得されます。other
の内容を持つコンテナを構築します。 alloc
が指定されない場合、アロケータは other
の持つアロケータからムーブ構築によって取得されます。init
の内容を持つコンテナを構築します。 unordered_map(init.begin(), init.end()) と同様です。目次 |
[編集]引数
alloc | - | このコンテナのすべてのメモリ確保に使用するアロケータ |
bucket_count | - | 初期化時に使用する最小バケット数。 指定されない場合は処理系定義のデフォルト値が使われます |
hash | - | 使用するハッシュ関数 |
equal | - | このコンテナのすべてのキー比較のために使用する比較関数 |
first, last | - | 要素をコピーする範囲 |
other | - | コンテナの要素を初期化するためのソースとして使用される別のコンテナ |
init | - | コンテナの要素を初期化するための初期化子リスト |
型の要件 | ||
-InputIt は LegacyInputIterator の要件を満たさなければなりません。 |
[編集]計算量
first
と last
の距離に比例。 ワーストケースではその二乗。other
のサイズに比例。alloc
が指定されていて alloc != other.get_allocator() の場合は比例。init
のサイズに比例。 ワーストケースではその二乗。[編集]例外
Allocator::allocate
の呼び出しは例外を投げるかもしれません。
[編集]ノート
other
を指す参照、ポインタ、イテレータ (終端イテレータは除く) は有効なまま残りますが、以後 *this 内の要素を指すようになります。 現行の標準ではこの保証は [container.requirements.general]/12 の包括的な文言によってなされていますが、より直接的な保証が LWG 2321 で検討されています。 [編集]例
#include <unordered_map>#include <vector>#include <bitset>#include <string>#include <utility> struct Key {std::string first;std::string second;}; struct KeyHash {std::size_t operator()(const Key& k)const{returnstd::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;}}; namespace std {template<>struct hash<Foo>{std::size_t operator()(const Foo &f)const{returnstd::hash<int>{}(f.val);}};} int main(){// デフォルトコンストラクタ。 空の map。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; // ムーブコンストラクタ。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 ={{{"John", "Doe"}, "example"}, {{"Mary", "Sue"}, "another"}}; //カスタム Key 型を持つコンストラクタのためのオプションその2。// クラス/構造体に対する const な == 演算子を定義し、// std 名前空間の std::hash 構造体を特殊化します。std::unordered_map<Foo, std::string> m7 ={{ Foo(1), "One"}, {2, "Two"}, {3, "Three"}}; //オプションその3: ラムダを用いる。// コンストラクタに初期バケット数を渡す必要があることに注意してください。struct Goo {int val;};auto hash =[](const Goo &g){returnstd::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 | the default constructor is explicit | made non-explicit |
[編集]関連項目
コンテナに値を代入します (パブリックメンバ関数) |