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

std::unordered_map

STD:无序[医]地图

Defined in header
template< class Key, class T, class Hash = std::hash<Key>, class KeyEqual = std::equal_to<Key>, class Allocator = std::allocator< std::pair<const Key, T> > > class unordered_map;(1)(since C++11)
namespace pmr { template <class Key, class T, class Hash = std::hash<Key>, class Pred = std::equal_to<Key>> using unordered_map = std::unordered_map<Key, T, Hash, Pred, std::pmr::polymorphic_allocator<std::pair<const Key,T>>>; }(2)(since C++17)

无序映射是一个关联容器,包含具有唯一键的键值对。元素的搜索、插入和删除具有平均恒定时间复杂度.

在内部,元素不是按任何特定的顺序排序,而是组织成桶。将元素放入哪个桶完全取决于其键的散列。这允许快速访问单个元素,因为一旦计算了散列,它就会引用元素被放置到的确切桶中。

std::unordered_map满足…的要求Container,,,AllocatorAwareContainer,,,UnorderedAssociativeContainer...

迭代器失效

OperationsInvalidated
All read only operations, swap, std::swapNever
clear, rehash, reserve, operator=Always
insert, emplace, emplace_hint, operator[]Only if causes rehash
eraseOnly to the element erased

注记

  • 交换函数不会使容器中的任何迭代器失效,但它们会使标志着交换区域结束的迭代器失效。

  • 对存储在容器中的键或数据的引用和指针仅通过擦除该元素而失效,即使在相应的迭代器失效时也是如此。

成员类型

Member typeDefinition
key_typeKey
mapped_typeT
value_typestd::pair<const Key, T>
size_typeUnsigned integer type (usually std::size_t)
difference_typeSigned integer type (usually std::ptrdiff_t)
hasherHash
key_equalKeyEqual
allocator_typeAllocator
referencevalue_type&
const_referenceconst value_type&
pointerstd::allocator_traits<Allocator>::pointer
const_pointerstd::allocator_traits<Allocator>::const_pointer
iteratorForwardIterator
const_iteratorConstant forward iterator
local_iteratorAn iterator type whose category, value, difference, pointer and reference types are the same as iterator. This iterator can be used to iterate through a single bucket but not across buckets
const_local_iteratorAn iterator type whose category, value, difference, pointer and reference types are the same as const_iterator. This iterator can be used to iterate through a single bucket but not across buckets
node_typea specialization of node handle representing a container node (since C++17)
insert_return_typetype describing the result of inserting a node_type, a specialization of template <class Iterator, class NodeType> struct /*unspecified*/ { Iterator position; bool inserted; NodeType node; }; instantiated with template arguments iterator and node_type. (since C++17)

成员函数

(constructor)constructs the unordered_map (public member function)
(destructor)destructs the unordered_map (public member function)
operator=assigns values to the container (public member function)
get_allocatorreturns the associated allocator (public member function)

迭代器

BEGINCBEGIN将迭代器返回到开头%28的公共成员函数%29

End cend将迭代器返回到End%28公共成员函数%29

容量

空检查容器是否为空%28公共成员函数%29

Size返回元素数%28公共成员函数%29

马克斯[医]Size返回元素的最大可能数%28公共成员函数%29

修饰符

清除内容%28公共成员功能%29

插入元素或节点%28,因为C++17%29%28公共成员函数%29

插入[医]或[医]如果键已经存在,则指定%28C++17%29插入一个元素或分配给当前元素

座落[医]提示使用提示%28公共成员函数%29就地构造元素。

试一试[医]嵌入%28C++17%29如果密钥不存在,则插入就地;如果密钥存在%28公共成员函数%29,则不执行任何操作。

擦除元素%28公共成员函数%29

交换交换内容%28公共成员函数%29

提取%28C++17%29从容器中提取节点%28公共成员函数%29

从另一个容器合并%28C++17%29个连接节点%28公共成员函数%29

查找

在访问指定元素时,使用边界检查%28公共成员函数%29

操作者。[]访问指定元素%28公共成员函数%29

Count返回匹配特定键的元素数%28公共成员函数%29

查找具有特定密钥%28公共成员函数%29的查找元素

平等[医]范围返回匹配特定键%28公共成员函数%29的元素的范围

桶接口

开始%28 int%29 cBEGIN%28 int%29返回一个迭代器到指定桶%28公共成员函数%29的开头

End%28 int%29 cend%28 int%29返回一个迭代器到指定桶%28公共成员函数%29的末尾

斗[医]Count返回桶数%28公共成员函数%29

马克斯[医]斗[医]COUNT返回最大桶数%28公共成员函数%29

斗[医]Size返回特定桶%28公共成员函数中的元素数%29

桶返回特定键%28公共成员函数%29的桶

散列策略

负载[医]因子返回每个桶的平均元素数%28公共成员函数%29

马克斯[医]负载[医]因子管理每个桶的最大平均元素数%28公共成员函数%29

重新哈希至少保留指定数量的桶。这将重新生成哈希表。28%公共成员职能%29

储备空间至少保留指定数量的元素。这将重新生成哈希表。28%公共成员职能%29

观察员

散列[医]函数返回函数用于散列键%28公共成员函数%29

键[医]Eq返回用于比较相等性%28公共成员函数%29的键的函数。

非会员职能

operator==operator!=compares the values in the unordered_map (function template)
std::swap(std::unordered_map) (C++11)specializes the std::swap algorithm (function template)

二次

#include <iostream> #include <string> #include <unordered_map> int main() { // Create an unordered_map of three strings (that map to strings) std::unordered_map<std::string, std::string> u = { {"RED","#FF0000"}, {"GREEN","#00FF00"}, {"BLUE","#0000FF"} }; // Iterate and print keys and values of unordered_map for( const auto& n : u ) { std::cout << "Key:[" << n.first << "] Value:[" << n.second << "]\n"; } // Add two new entries to the unordered_map u["BLACK"] = "#000000"; u["WHITE"] = "#FFFFFF"; // Output values by key std::cout << "The HEX of color RED is:[" << u["RED"] << "]\n"; std::cout << "The HEX of color BLACK is:[" << u["BLACK"] << "]\n"; return 0; }

二次

产出:

二次

Key:[RED] Value:[#FF0000] Key:[BLUE] Value:[#0000FF] Key:[GREEN] Value:[#00FF00] The HEX of color RED is:[#FF0000] The HEX of color BLACK is:[#000000]

二次

© cppreference.com

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

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