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