Namespaces
Variants

std::unordered_map<Key,T,Hash,KeyEqual,Allocator>:: unordered_map

From cppreference.net

(注:このHTML要素には翻訳対象のテキストコンテンツが含まれていないため、元の構造を保持したまま出力します)
(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 ( ) ,
const key_equal & equal = key_equal ( ) ,

const Allocator & alloc = Allocator ( ) ) ;
(2) (C++11以降)
unordered_map ( size_type bucket_count,

const Allocator & alloc )

: unordered_map ( bucket_count, Hash ( ) , key_equal ( ) , alloc ) { }
(3) (C++14以降)
unordered_map ( size_type bucket_count,

const Hash & hash,
const Allocator & alloc )

: unordered_map ( bucket_count, hash, key_equal ( ) , alloc ) { }
(4) (C++14以降)
explicit unordered_map ( const Allocator & alloc ) ;
(5) (C++11以降)
template < class InputIt >

unordered_map ( InputIt first, InputIt last,
size_type bucket_count = /* 未指定 */ ,
const Hash & hash = Hash ( ) ,
const key_equal & equal = key_equal ( ) ,

const Allocator & alloc = Allocator ( ) ) ;
(6) (C++11以降)
template < class InputIt >

unordered_map ( InputIt first, InputIt last,
size_type bucket_count,
const Allocator & alloc )
: unordered_map ( first, last,

bucket_count, Hash ( ) , key_equal ( ) , alloc ) { }
(7) (C++14以降)
template < class InputIt >

unordered_map ( InputIt first, InputIt last,
size_type bucket_count,
const Hash & hash,
const Allocator & alloc )
: unordered_map ( first, last,

bucket_count, hash, key_equal ( ) , alloc ) { }
(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 = /* 未指定 */ ,
const Hash & hash = Hash ( ) ,
const key_equal & equal = key_equal ( ) ,

const Allocator & alloc = Allocator ( ) ) ;
(13) (C++11以降)
unordered_map ( std:: initializer_list < value_type > init,

size_type bucket_count,
const Allocator & alloc )
: unordered_map ( init, bucket_count,

Hash ( ) , key_equal ( ) , alloc ) { }
(14) (C++14以降)
unordered_map ( std:: initializer_list < value_type > init,

size_type bucket_count,
const Hash & hash,
const Allocator & alloc )
: unordered_map ( init, bucket_count,

hash, key_equal ( ) , alloc ) { }
(15) (C++14以降)
template < container-compatible-range < value_type > R >

unordered_map ( std:: from_range_t , R && rg,
size_type bucket_count = /* 詳細を参照 */ ,
const Hash & hash = Hash ( ) ,
const key_equal & equal = key_equal ( ) ,

const Allocator & alloc = Allocator ( ) ) ;
(16) (C++23以降)
template < container-compatible-range < value_type > R >

unordered_map ( std:: from_range_t , R && rg,
size_type bucket_count,
const Allocator & alloc )
: unordered_map ( std:: from_range , std:: forward < R > ( rg ) ,

bucket_count, Hash ( ) , key_equal ( ) , alloc ) { }
(17) (C++23以降)
template < container-compatible-range < value_type > R >

unordered_map ( std:: from_range_t , R && rg,
size_type bucket_count,
const Hash & hash,
const Alloc & alloc )
: unordered_map ( std:: from_range , std:: forward < R > ( rg ) ,

bucket_count, hash, key_equal ( ) , alloc ) { }
(18) (C++23以降)

様々なデータソースから新しいコンテナを構築します。オプションでユーザー指定の bucket_count を最小バケット数として使用し、 hash をハッシュ関数、 equal をキー比較関数、 alloc をアロケータとして使用します。

1-5) 空のコンテナを構築します。 max_load_factor() 1.0 に設定します。デフォルトコンストラクタでは、バケット数は未指定です。
6-8) 範囲 [ first , last ) の内容でコンテナを構築します。 max_load_factor() 1.0 に設定します。範囲内の複数の要素が同等のキーを持つ場合、どの要素が挿入されるかは未規定です( LWG2844 保留中)。
9,10) コピーコンストラクタ。コンテナを other の内容のコピーで構築し、負荷係数、述語、およびハッシュ関数もコピーします。 alloc が提供されない場合、アロケータは std:: allocator_traits < allocator_type > :: select_on_container_copy_construction ( other. get_allocator ( ) ) を呼び出すことで取得されます。

テンプレートパラメータ Allocator は、 クラステンプレート引数推論 で使用される場合、最初の引数からのみ推論されます。

(C++23以降)
11,12) ムーブコンストラクタ . other の内容をムーブセマンティクスを使用してコンテナを構築します。 alloc が提供されない場合、アロケータは other に属するアロケータからのムーブ構築によって取得されます。

テンプレートパラメータ Allocator は、 クラステンプレート引数推論 で使用される際、最初の引数からのみ推論されます。

(C++23以降)
13-15) 初期化子リストコンストラクタ . 初期化子リスト init の内容でコンテナを構築する。 unordered_map ( init. begin ( ) , init. end ( ) ) と同じ。
16-18) 範囲 rg の内容でコンテナを構築します。範囲内の複数の要素が同等と比較されるキーを持つ場合、どの要素が挿入されるかは未規定です( LWG2844 保留中)。

目次

パラメータ

alloc - このコンテナのすべてのメモリ割り当てに使用するアロケータ
bucket_count - 初期化時に使用する最小バケット数。指定されない場合、未規定のデフォルト値が使用される
hash - 使用するハッシュ関数
equal - このコンテナのすべてのキー比較に使用する比較関数
first, last - コピーする要素のソース 範囲 を定義するイテレータのペア
rg - コンテナ互換範囲 、すなわち要素が value_type に変換可能な input_range
other - コンテナの要素を初期化するためのソースとして使用する別のコンテナ
init - コンテナの要素を初期化するための初期化子リスト
型要件
-
InputIt LegacyInputIterator の要件を満たさなければならない

計算量

1-5) 定数。
6-8) 平均ケース線形(すなわち O(N) 、ここで N std:: distance ( first, last ) )、最悪ケース二次、すなわち O(N 2 )
9,10) サイズに対して線形 other .
11,12) 定数。もし alloc が与えられ、かつ alloc ! = other. get_allocator ( ) の場合、線形。
13-15) 平均ケース O(N) ( N std:: size ( init ) )、最悪ケース O(N 2 )
16-18) 平均ケース O(N) ( N ranges:: distance ( rg ) )、最悪ケース O(N 2 )

例外

Allocator::allocate への呼び出しは例外をスローする可能性があります。

注記

After container move construction (overload ( 11,12 ) ), references, pointers, and iterators (other than the end iterator) to other remain valid, but refer to elements that are now in * this . The current standard makes this guarantee via the blanket statement in [container.reqmts]/67 , and a more direct guarantee is under consideration via LWG issue 2321 .

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 ) のセマンティクスが規定されていなかった 規定された

関連項目

コンテナに値を代入する
(公開メンバ関数)