命名空间
变体
操作

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

来自 cppreference.cn
< cpp‎ | 容器‎ | 无序映射
 
 
 
 
(1)
unordered_map()
    : unordered_map(size_type(/* unspecified */)) {}
(since C++11)
(until C++20)
unordered_map();
(since C++20)
explicit unordered_map( size_type bucket_count,

                        const Hash& hash = Hash(),
                        const key_equal& equal = key_equal(),

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

               const Allocator& alloc )

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

               const Hash& hash,
               const Allocator& alloc )

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

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

               const Allocator& alloc = Allocator() );
(6) (since 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) (since 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) (since C++14)
unordered_map( const unordered_map& other );
(9) (since C++11)
unordered_map( const unordered_map& other, const Allocator& alloc );
(10) (since C++11)
unordered_map( unordered_map&& other );
(11) (since C++11)
unordered_map( unordered_map&& other, const Allocator& alloc );
(12) (since C++11)
unordered_map( std::initializer_list<value_type> init,

               size_type bucket_count = /* unspecified */,
               const Hash& hash = Hash(),
               const key_equal& equal = key_equal(),

               const Allocator& alloc = Allocator() );
(13) (since 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) (since 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) (since C++14)
template< container-compatible-range<value_type> R >

unordered_map( std::from_range_t, R&& rg,
               size_type bucket_count = /* see description */,
               const Hash& hash = Hash(),
               const key_equal& equal = key_equal(),

               const Allocator& alloc = Allocator() );
(16) (since 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) (since 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) (since C++23)

从各种数据源构造新的容器。 可选地使用用户提供的 bucket_count 作为要创建的最小桶数,hash 作为哈希函数,equal 作为比较键的函数,以及 alloc 作为分配器。

1-5) 构造空容器。 将 max_load_factor() 设置为 1.0。 对于默认构造函数,桶的数量是未指定的。
6-8) 使用范围 [firstlast) 的内容构造容器。 将 max_load_factor() 设置为 1.0。 如果范围内有多个元素的键比较相等,则未指定插入哪个元素(待定 LWG2844)。
9,10) 复制构造函数。 使用 other 内容的副本构造容器,同时复制负载因子、谓词和哈希函数。 如果未提供 alloc,则通过调用 std::allocator_traits<allocator_type>::select_on_container_copy_construction(other.get_allocator()) 来获取分配器。

模板参数 Allocator 仅在使用 类模板参数推导 时才从第一个参数推导。

(since C++23)
11,12) 移动构造函数。 使用移动语义和 other 的内容构造容器。 如果未提供 alloc,则通过从属于 other 的分配器进行移动构造来获取分配器。

模板参数 Allocator 仅在使用 类模板参数推导 时才从第一个参数推导。

(since C++23)
13-15) 初始化列表构造函数。 使用初始化列表 init 的内容构造容器,与 unordered_map(init.begin(), init.end()) 相同。
16-18) 使用 rg 的内容构造容器。 如果范围内有多个元素的键比较相等,则未指定插入哪个元素(待定 LWG2844)。

目录

[编辑] 参数

alloc - 用于此容器的所有内存分配的分配器
bucket_count - 初始化时使用的最小桶数。 如果未指定,则使用未指定的默认值
hash - 要使用的哈希函数
equal - 用于此容器的所有键比较的比较函数
first, last - 定义要复制的元素的源 范围 的迭代器对
rg - 一个 容器兼容范围,即,一个 input_range,其元素可转换为 value_type
other - 另一个容器,用作初始化此容器元素的源
init - 用于初始化此容器元素的初始化列表
类型要求
-
InputIt 必须满足 LegacyInputIterator 的要求。

[编辑] 复杂度

1-5) 常数。
6-8) 平均情况线性 (即 O(N),其中 Nstd::distance(first, last)),最坏情况二次方,即 O(N2)
9,10)other 的大小成线性关系。
11,12) 常数。 如果给定 alloc 并且 alloc != other.get_allocator(),则为线性。
13-15) 平均情况 O(N)Nstd::size(init)),最坏情况 O(N2)
16-18) 平均情况 O(N)Nranges::distance(rg)),最坏情况 O(N2)

[编辑] 异常

调用 Allocator::allocate 可能会抛出异常。

[编辑] 注解

在容器移动构造后(重载 (4)),对 other 的引用、指针和迭代器(末尾迭代器除外)仍然有效,但引用现在位于 *this 中的元素。 当前标准通过 [container.reqmts]/67 中的一概而论的声明做出此保证,并且正在通过 LWG issue 2321 考虑更直接的保证。

尽管在 C++23 之前不是正式要求的,但某些实现已经在较早的模式中将模板参数 Allocator 放入 非推导上下文 中。

特性测试 Std 特性
__cpp_lib_containers_ranges 202202L (C++23) 范围感知 构造和插入;重载 (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()
{
    // default constructor: empty map
    std::unordered_map<std::string, std::string> m1;
 
    // list constructor
    std::unordered_map<int, std::string> m2 =
    {
        {1, "foo"},
        {3, "bar"},
        {2, "baz"}
    };
 
    // copy constructor
    std::unordered_map<int, std::string> m3 = m2;
 
    // move constructor
    std::unordered_map<int, std::string> m4 = std::move(m2);
 
    // range constructor
    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());
 
    // Option 1 for a constructor with a custom Key type
    // Define the KeyHash and KeyEqual structs and use them in the template
    std::unordered_map<Key, std::string, KeyHash, KeyEqual> m6 =
    {
        {{"John", "Doe"}, "example"},
        {{"Mary", "Sue"}, "another"}
    };
 
    // Option 2 for a constructor with a custom Key type.
    // Define a const == operator for the class/struct and specialize std::hash
    // structure in the std namespace
    std::unordered_map<Foo, std::string> m7 =
    {
        {Foo(1), "One"}, {2, "Two"}, {3, "Three"}
    };
 
    // Option 3: Use lambdas
    // Note that the initial bucket count has to be passed to the constructor
    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) 是显式的 改为非显式
LWG 2230 C++11 重载 (13) 的语义未指定 已指定

[编辑] 参见

为容器赋值
(公共成员函数) [编辑]