Namespaces
Variants

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

From cppreference.net

(1)
unordered_map ( )
: unordered_map ( size_type ( /* 未指定 */ ) ) { }
(自 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 < <a

从多种数据源构造新容器。可选择使用用户提供的 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 问题 2321 .

尽管在C++23之前并非正式要求,但某些实现在早期模式中已将模板参数 Allocator 置于 非推导语境 中。

功能测试 标准 功能
__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()
{
    // 默认构造函数:空映射
    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());
    // 自定义键类型的构造函数选项1
    // 定义 KeyHash 和 KeyEqual 结构体并在模板中使用它们
    std::unordered_map<Key, std::string, KeyHash, KeyEqual> m6 =
    {
        {{"John", "Doe"}, "example"},
        {{"Mary", "Sue"}, "another"}
    };
    // 自定义键类型的构造函数选项2
    // 为类/结构体定义 const == 运算符并在 std 命名空间中特化 std::hash 结构体
    std::unordered_map<Foo, std::string> m7 =
    {
        {Foo(1), "One"}, {2, "Two"}, {3, "Three"}
    };
    // 选项3:使用 lambda 表达式
    // 注意初始桶数量必须传递给构造函数
    struct Goo { int val; };
    auto hash = [](const Goo &g){ return std::hash<int>{}(g.val<span class="br0

缺陷报告

以下行为变更缺陷报告被追溯应用于先前发布的C++标准。

缺陷报告 适用范围 发布时行为 正确行为
LWG 2193 C++11 默认构造函数 ( 1 ) 为显式 改为非显式
LWG 2230 C++11 重载版本 ( 13 ) 的语义未作规定 已作规定

另请参阅

为容器赋值
(公开成员函数)