unordered_map::unordered_map

构造容器对象。

unordered_map(
    const unordered_map& Right
);
explicit unordered_map(
    size_type Bucket_count = N0,
    const Hash& Hash = Hash(),
    const Comp& Comp = Comp(),
    const Allocator& Al = Allocator()
);
unordered_map(
    unordered_map&& Right
);
unordered_map(
    initializer_list<Type> IList
);
unordered_map(
    initializer_list<Type> IList, 
    size_type Bucket_count
);
unordered_map(
    initializer_list<Type> IList, 
    size_type Bucket_count, 
    const Hash& Hash
);
unordered_map(
    initializer_list<Type> IList, 
    size_type Bucket_count, 
    const Hash&  Hash,
    KeyEqual& equal
);
unordered_map(
    initializer_list<Type> IList, 
    size_type Bucket_count,
    const Hash&  Hash,
    KeyEqual& Equal
    const Allocator& Al
);
template<class InIt>
    unordered_map(
        InputIterator First, 
        InputIterator Last,
        size_type Bucket_count = N0,
        const Hash& Hash = Hash(),
        const Comp& Comp = Comp(),
        const Allocator& Al = Alloc()
    );

参数

参数

描述

Al

要存储的分配器对象。

Comp

要存储的比较函数对象。

Hash

要存储的哈希函数对象。

Bucket_count

存储桶的最少数量。

Right

要复制的容器。

First

Last

IList

包含要复制的元素的 initializer_list。

备注

第一个构造函数指定通过 right 控制的序列副本。 第二个构造函数指定空的受控序列。 第三个构造函数插入元素值 [first, last) 的序列。 第四个构造函数通过移动 right 指定序列副本。

所有构造函数还初始化若干存储的值。 对于复制构造函数,值从 Right 获取。 否则:

存储桶的最少数量是参数 Bucket_count(如果有);否则它为默认值,即此处描述为实现定义的值 N0。

哈希函数对象是参数 Hash(如果有);否则为 Hash()。

比较函数对象是参数 Comp(如果有);否则为 Pred()。

分配器对象是参数 Al(如果有);否则为 Alloc()。

示例

 

// std__unordered_map__unordered_map_construct.cpp 
// compile with: /EHsc 
#include <unordered_map> 
#include <iostream> 
#include <initializer_list>

using namespace std;

using Mymap = unordered_map<char, int>;

int main()
{
    Mymap c1;

    c1.insert(Mymap::value_type('a', 1));
    c1.insert(Mymap::value_type('b', 2));
    c1.insert(Mymap::value_type('c', 3));

    // display contents " [c 3] [b 2] [a 1]" 
    for (const auto& c : c1) {
        cout << " [" << c.first << ", " << c.second << "]";
    }
    cout << endl;

    Mymap c2(8,
        tr1::hash<char>(),
        equal_to<char>(),
        allocator<pair<const char, int> >());

    c2.insert(Mymap::value_type('d', 4));
    c2.insert(Mymap::value_type('e', 5));
    c2.insert(Mymap::value_type('f', 6));

    // display contents " [f 6] [e 5] [d 4]" 
    for (const auto& c : c2) {
        cout << " [" << c.first << ", " << c.second << "]";
    }
    cout << endl;

    Mymap c3(c1.begin(),
        c1.end(),
        8,
        tr1::hash<char>(),
        equal_to<char>(),
        allocator<pair<const char, int> >());

    // display contents " [c 3] [b 2] [a 1]" 
    for (const auto& c : c3) {
        cout << " [" << c.first << ", " << c.second << "]";
    }
    cout << endl;

    Mymap c4(move(c3));

    // display contents " [c 3] [b 2] [a 1]" 
    for (const auto& c : c4) {
        cout << " [" << c.first << ", " << c.second << "]";
    }
    cout << endl;
    cout << endl;

    // Construct with an initializer_list
    unordered_map<int, char> c5({ { 5, 'g' }, { 6, 'h' }, { 7, 'i' }, { 8, 'j' } });
    for (const auto& c : c5) {
        cout << " [" << c.first << ", " << c.second << "]";
    }
    cout << endl;

    // Initializer_list plus size
    unordered_map<int, char> c6({ { 5, 'g' }, { 6, 'h' }, { 7, 'i' }, { 8, 'j' } }, 4);
    for (const auto& c : c1) {
        cout << " [" << c.first << ", " << c.second << "]";
    }
    cout << endl;
    cout << endl;

    // Initializer_list plus size and hash
    unordered_map<int, char, tr1::hash<char>> c7(
        { { 5, 'g' }, { 6, 'h' }, { 7, 'i' }, { 8, 'j' } }, 
        4, 
        tr1::hash<char>()
    );
   
    for (const auto& c : c1) {
        cout << " [" << c.first << ", " << c.second << "]";
    }
    cout << endl;

    // Initializer_list plus size, hash, and key_equal
    unordered_map<int, char, tr1::hash<char>, equal_to<char>> c8(
        { { 5, 'g' }, { 6, 'h' }, { 7, 'i' }, { 8, 'j' } }, 
        4, 
        tr1::hash<char>(), 
        equal_to<char>()
    );
    
    for (const auto& c : c1) {
        cout << " [" << c.first << ", " << c.second << "]";
    }
    cout << endl;

    // Initializer_list plus size, hash, key_equal, and allocator
    unordered_map<int, char, tr1::hash<char>, equal_to<char>> c9(
        { { 5, 'g' }, { 6, 'h' }, { 7, 'i' }, { 8, 'j' } },
        4,
        tr1::hash<char>(),
        equal_to<char>(),
        allocator<pair<const char, int> >()
    );
    
    for (const auto& c : c1) {
        cout << " [" << c.first << ", " << c.second << "]";
    }
    cout << endl;
}
  

要求

标头:<unordered_map>

命名空间: std

请参见

参考

<unordered_map>

unordered_map 类

其他资源

unordered_map 成员