定义于头文件 <unordered_map>
template< class Key, | (1) | (C++11 起) |
namespace pmr { template <class Key, | (2) | (C++17 起) |
unordered_map 是关联容器,含有带唯一键的键-值 pair 。搜索、插入和元素移除拥有平均常数时间复杂度。
元素在内部不以任何特定顺序排序,而是组织进桶中。元素放进哪个桶完全依赖于其键的哈希。这允许对单独元素的快速访问,因为一旦计算哈希,则它准确指代元素所放进的桶。
观察器
返回用于对关键哈希的函数
std::unordered_map<Key,T,Hash,KeyEqual,Allocator>::hash_function
hasher hash_function() const; | (C++11 起) |
返回对关键哈希的函数。
参数
(无)
返回值
哈希函数。
复杂度
常数。
返回用于比较键的相等性的函数
std::unordered_map<Key,T,Hash,KeyEqual,Allocator>::key_eq
key_equal key_eq() const; | (C++11 起) |
返回比较关键相等性的函数。
参数
(无)
返回值
关键比较函数。
复杂度
常数。
非成员函数
比较 unordered_map 中的值
operator==,!=(std::unordered_map)
template< class Key, class T, class Hash, class KeyEqual, class Allocator > bool operator==( const unordered_map<Key,T,Hash,KeyEqual,Allocator>& lhs, const unordered_map<Key,T,Hash,KeyEqual,Allocator>& rhs ); | (1) | |
template< class Key, class T, class Hash, class KeyEqual, class Allocator > bool operator!=( const unordered_map<Key,T,Hash,KeyEqual,Allocator>& lhs, const unordered_map<Key,T,Hash,KeyEqual,Allocator>& rhs ); | (2) |
比较二个无序容器的内容。
若下列条件成立则二个无序容器 lhs
与 rhs
相等:
- lhs.size() == rhs.size()
- 从 lhs.equal_range(lhs_eq1) 获得的每组等价元素
[lhs_eq1, lhs_eq2)
拥有在另一容器中从 rhs.equal_range(rhs_eq1) 获得的对应等价元素组[rhs_eq1, rhs_eq2)
,且它们拥有下列属性:
- std::distance(lhs_eq1, lhs_eq2) == std::distance(rhs_eq1, rhs_eq2) 。
- std::is_permutation(lhs_eq1, lhs_eq2, rhs_eq1) == true 。
若 Key
或 T
不可相等比较 (EqualityComparable) 则行为未定义。
若 hash_function()
和 key_eq()
(C++20 前)key_eq()
(C++20 起) 在 lhs
和 rhs
上拥有不相同的行为,或若 Key
的 operator== 不是对于 key_eq()
所引入的等价关键组的细分(即若用 operator== 比较相等的二个元素落入不同划分),则行为未定义。
参数
lhs, rhs | - | 要比较的无序容器 |
返回值
1) 若容器内容相等则为 true ,否则为 false 。
2) 若容器内容不相等则为 true ,否则为 false 。
复杂度
调用 value_type
上的 operator== 、调用 key_eq 所返回的谓词,及调用 hash_function 所返回的哈希器的次数,平均情况下与 N 成比例,最坏情况下与 N2 成比例,其中 N 是容器大小。
特化 std::swap 算法
std::swap(std::unordered_map
template< class Key, class T, class Hash, class KeyEqual, class Alloc > void swap( unordered_map<Key,T,Hash,KeyEqual,Alloc>& lhs, unordered_map<Key,T,Hash,KeyEqual,Alloc>& rhs ); | (C++11 起) (C++17 前) | |
template< class Key, class T, class Hash, class KeyEqual, class Alloc > void swap( unordered_map<Key,T,Hash,KeyEqual,Alloc>& lhs, unordered_map<Key,T,Hash,KeyEqual,Alloc>& rhs ) noexcept(/* see below */); | (C++17 起) |
为 std::unordered_map 特化 std::swap 算法。交换 lhs
与 rhs
的内容。调用 lhs.swap(rhs) 。
参数
lhs, rhs | - | 要交换内容的容器 |
返回值
(无)
复杂度
常数。
异常
noexcept 规定: noexcept(noexcept(lhs.swap(rhs))) | (C++17 起) |
调用示例
#include <iostream>
#include <forward_list>
#include <string>
#include <iterator>
#include <algorithm>
#include <functional>
#include <unordered_map>
#include <time.h>
using namespace std;
struct Cell
{
int x;
int y;
Cell() = default;
Cell(int a, int b): x(a), y(b) {}
Cell &operator +=(const Cell &cell)
{
x += cell.x;
y += cell.y;
return *this;
}
Cell &operator +(const Cell &cell)
{
x += cell.x;
y += cell.y;
return *this;
}
Cell &operator *(const Cell &cell)
{
x *= cell.x;
y *= cell.y;
return *this;
}
Cell &operator ++()
{
x += 1;
y += 1;
return *this;
}
bool operator <(const Cell &cell) const
{
if (x == cell.x)
{
return y < cell.y;
}
else
{
return x < cell.x;
}
}
bool operator >(const Cell &cell) const
{
if (x == cell.x)
{
return y > cell.y;
}
else
{
return x > cell.x;
}
}
bool operator ==(const Cell &cell) const
{
return x == cell.x && y == cell.y;
}
};
struct myCompare
{
bool operator()(const int &a, const int &b)
{
return a < b;
}
};
std::ostream &operator<<(std::ostream &os, const Cell &cell)
{
os << "{" << cell.x << "," << cell.y << "}";
return os;
}
std::ostream &operator<<(std::ostream &os, const std::pair<Cell, string> &pCell)
{
os << pCell.first << "-" << pCell.second;
return os;
}
struct CHash
{
size_t operator()(const Cell& cell) const
{
size_t thash = std::hash<int>()(cell.x) | std::hash<int>()(cell.y);
// std::cout << "CHash: " << thash << std::endl;
return thash;
}
};
struct CEqual
{
bool operator()(const Cell &a, const Cell &b) const
{
return a.x == b.x && a.y == b.y;
}
};
int main()
{
std::cout << std::boolalpha;
std::mt19937 g{std::random_device{}()};
srand((unsigned)time(NULL));
auto generate = []()
{
int n = std::rand() % 10 + 110;
Cell cell{n, n};
return std::pair<Cell, string>(cell, std::to_string(n));
};
std::unordered_map<Cell, string, CHash, CEqual> unordered_map1;
while (unordered_map1.size() < 5)
{
//6) 插入来自 initializer_list ilist 的元素。若范围中的多个元素拥有比较等价的关键,则插入哪个元素是未指定的
unordered_map1.insert({generate()});
}
std::cout << "unordered_map1: ";
std::copy(unordered_map1.begin(), unordered_map1.end(),
std::ostream_iterator<pair<Cell, string>>(std::cout, " "));
std::cout << std::endl;
std::unordered_map<Cell, string, CHash, CEqual> unordered_map2;
while (unordered_map2.size() < 5)
{
//6) 插入来自 initializer_list ilist 的元素。若范围中的多个元素拥有比较等价的关键,则插入哪个元素是未指定的
unordered_map2.insert({generate()});
}
std::cout << "unordered_map2: ";
std::copy(unordered_map2.begin(), unordered_map2.end(),
std::ostream_iterator<pair<Cell, string>>(std::cout, " "));
std::cout << std::endl;
std::cout << std::endl;
//比较二个无序容器的内容。
std::cout << "unordered_map1 == unordered_map2: " << (unordered_map1 == unordered_map2) << std::endl;
std::cout << "unordered_map1 != unordered_map2: " << (unordered_map1 != unordered_map2) << std::endl;
std::cout << std::endl;
//为 std::unordered_map 特化 std::swap 算法。交换 lhs 与 rhs 的内容。调用 lhs.swap(rhs) 。
std::swap(unordered_map1, unordered_map2);
std::cout << "after swap: " << std::endl;
std::cout << "unordered_map1: ";
std::copy(unordered_map1.begin(), unordered_map1.end(),
std::ostream_iterator<pair<Cell, string>>(std::cout, " "));
std::cout << std::endl;
std::cout << "unordered_map2: ";
std::copy(unordered_map2.begin(), unordered_map2.end(),
std::ostream_iterator<pair<Cell, string>>(std::cout, " "));
std::cout << std::endl;
return 0;
}