在线文档教程
C++
容器 | Containers

std::unordered_map::unordered_map

STD:无序[医]地图::无序[医]地图

(1)
explicit unordered_map( size_type bucket_count = /*implementation-defined*/, const Hash& hash = Hash(), const KeyEqual& equal = KeyEqual(), const Allocator& alloc = Allocator() (since C++11) (until C++14)
unordered_map() : unordered_map( size_type(/*implementation-defined*/) ) {} explicit unordered_map( size_type bucket_count, const Hash& hash = Hash(), const KeyEqual& equal = KeyEqual(), const Allocator& alloc = Allocator() (since C++14)
unordered_map( size_type bucket_count, const Allocator& alloc ) : unordered_map(bucket_count, Hash(), KeyEqual(), alloc) {} unordered_map( size_type bucket_count, const Hash& hash, const Allocator& alloc ) : unordered_map(bucket_count, hash, KeyEqual(), alloc) {}(1)(since C++14)
explicit unordered_map( const Allocator& alloc (1)(since C++11)
template< class InputIt > unordered_map( InputIt first, InputIt last, size_type bucket_count = /*implementation-defined*/, const Hash& hash = Hash(), const KeyEqual& equal = KeyEqual(), const Allocator& alloc = Allocator() (2)(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(), KeyEqual(), alloc) {}(2)(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, KeyEqual(), alloc) {}(2)(since C++14)
unordered_map( const unordered_map& other (3)(since C++11)
unordered_map( const unordered_map& other, const Allocator& alloc (3)(since C++11)
unordered_map( unordered_map&& other (4)(since C++11)
unordered_map( unordered_map&& other, const Allocator& alloc (4)(since C++11)
unordered_map( std::initializer_list<value_type> init, size_type bucket_count = /*implementation-defined*/, const Hash& hash = Hash(), const KeyEqual& equal = KeyEqual(), const Allocator& alloc = Allocator() (5)(since C++11)
unordered_map( std::initializer_list<value_type> init, size_type bucket_count, const Allocator& alloc ) : unordered_map(init, bucket_count, Hash(), KeyEqual(), alloc) {}(5)(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, KeyEqual(), alloc) {}(5)(since C++14)

从各种数据源构造新容器。可选地使用所提供的用户bucket_count作为要创建的最少数量的桶,hash作为散列函数,equal作为比较键和alloc作为分配器。

1%29构造空容器。集max_load_factor()到1.0。对于默认构造函数,桶的数量是实现定义的。

2%29构造包含范围内容的容器。[first, last).成套max_load_factor()如果范围中的多个元素具有比较等价的键,则未指定插入了%28挂起的元素LWG2844%29

3%29复制构造函数。的内容的副本构造容器。other,复制加载因子、谓词和哈希函数。如果alloc不提供,则通过调用std::allocator_traits<allocator_type>::select_on_container_copy_construction(other.get_allocator())...

4%29移动构造函数。的内容构造容器。other使用移动语义。如果alloc如果不提供分配器,则通过移动构造从属于other...

5%29使用初始化程序列表的内容构造容器。init,和无序一样[医]映射%28 init.start%28%29,init.end%28%29%29。

参数

alloc-allocator to use for all memory allocations of this container
bucket_count-minimal number of buckets to use on initialization. If it is not specified, implementation-defined default value is used
hash-hash function to use
equal-comparison function to use for all key comparisons of this container
first, last-the range to copy the elements from
other-another container to be used as source to initialize the elements of the container with
init-initializer list to initialize the elements of the container with

类型要求

-输入必须符合输入器的要求。

复杂性

1%29常数

2%29平均情况下线性最坏情况二次之间的距离firstlast

3%29线性other

4%29常数。如果alloc被赋予和alloc != other.get_allocator(),然后是线性的。

5%29平均线性最坏情况二次型init

注记

在容器移动构造%28重载%284%29%29之后,引用、指针和迭代器%28---other保持有效,但引用当前在*this.现行标准通过第23.2.1节中的总括声明作出这一保证。集装箱。所需经费/12,目前正在考虑通过以下方式提供更直接的担保:lwg 2321...

二次

#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() { // 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 }

二次

另见

operator=assigns values to the container (public member function)

© cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

http://en.cppreference.com/w/cpp/container/unorder[医]地图/无序[医]地图