定义于头文件 <unordered_set>
template< class Key, | (1) | (C++11 起) |
namespace pmr { template <class Key, | (2) | (C++17 起) |
unordered_multiset 是关联容器,含有可能非唯一 Key 类型对象的集合。搜索、插入和移除拥有平均常数时间复杂度。
元素在内部并不以任何顺序排序,只是被组织到桶中。元素被放入哪个桶完全依赖其值的哈希。这允许快速访问单独的元素,因为一旦计算哈希,它就指代放置该元素的准确的桶。
不要求此容器的迭代顺序稳定(故例如 std::equal 不能用于比较二个 std::unordered_multiset
),除了关键比较等价(以 key_eq() 为比较器比较相等)的每组元素组成迭代顺序中的相接子范围,它可用 equal_range() 访问。
观察器
返回用于对关键哈希的函数
std::unordered_multiset<Key,Hash,KeyEqual,Allocator>::hash_function
hasher hash_function() const; | (C++11 起) |
返回对关键哈希的函数。
参数
(无)
返回值
哈希函数。
复杂度
常数。
返回用于比较键的相等性的函数
std::unordered_multiset<Key,Hash,KeyEqual,Allocator>::key_eq
key_equal key_eq() const; | (C++11 起) |
返回比较关键相等性的函数。
参数
(无)
返回值
关键比较函数。
复杂度
常数。
非成员函数
比较 unordered_multiset 中的值
operator==,!=(std::unordered_multiset)
template< class Key, class Hash, class KeyEqual, class Allocator > bool operator==( const unordered_multiset<Key,Hash,KeyEqual,Allocator>& lhs, const unordered_multiset<Key,Hash,KeyEqual,Allocator>& rhs ); | (1) | |
template< class Key, class Hash, class KeyEqual, class Allocator > bool operator!=( const unordered_multiset<Key,Hash,KeyEqual,Allocator>& lhs, const unordered_multiset<Key,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
不可相等比较 (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 所返回的哈希器的次数,平均情况下与 ΣSi2 成比例,其中 S 是第 i 个等价关键组的大小。最坏情况下与 N2 成比例,其中 N 是容器大小。若每个等价关键组中元素以相同顺序排列(在容器互为副本时发生),则平均情况变为与 N 成比例。
特化 std::swap 算法
std::swap(std::unordered_multiset)
template< class Key, class Hash, class KeyEqual, class Alloc > void swap( unordered_multiset<Key,Hash,KeyEqual,Alloc>& lhs, unordered_multiset<Key,Hash,KeyEqual,Alloc>& rhs ); | (C++11 起) (C++17 前) | |
template< class Key, class Hash, class KeyEqual, class Alloc > void swap( unordered_multiset<Key,Hash,KeyEqual,Alloc>& lhs, unordered_multiset<Key,Hash,KeyEqual,Alloc>& rhs ) noexcept(/* see below */); | (C++17 起) |
为 std::unordered_multiset 特化 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_set>
#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<const int, Cell> &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 + 100;
Cell cell{n, n};
return cell;
};
std::unordered_multiset<Cell, CHash, CEqual> unordered_multiset1;
while (unordered_multiset1.size() < 6)
{
//6) 插入来自 initializer_list ilist 的元素。
//若范围中的多个元素拥有比较等价的关键,则插入哪个元素是未指定的
unordered_multiset1.insert({generate()});
}
std::cout << "unordered_multiset1: ";
std::copy(unordered_multiset1.begin(), unordered_multiset1.end(),
std::ostream_iterator<Cell>(std::cout, " "));
std::cout << std::endl;
std::unordered_multiset<Cell, CHash, CEqual> unordered_multiset2;
while (unordered_multiset2.size() < 6)
{
//6) 插入来自 initializer_list ilist 的元素。
//若范围中的多个元素拥有比较等价的关键,则插入哪个元素是未指定的
unordered_multiset2.insert({generate()});
}
std::cout << "unordered_multiset2: ";
std::copy(unordered_multiset2.begin(), unordered_multiset2.end(),
std::ostream_iterator<Cell>(std::cout, " "));
std::cout << std::endl;
std::cout << std::endl;
//比较二个无序容器的内容。
std::cout << "unordered_multiset1 == unordered_multiset2: "
<< (unordered_multiset1 == unordered_multiset2) << std::endl;
std::cout << "unordered_multiset1 != unordered_multiset2: "
<< (unordered_multiset1 != unordered_multiset2) << std::endl;
std::cout << std::endl;
//为 std::unordered_multiset 特化 std::swap 算法。交换 lhs 与 rhs 的内容。调用 lhs.swap(rhs) 。
std::swap(unordered_multiset1, unordered_multiset2);
std::cout << "after swap: " << std::endl;
std::cout << "unordered_multiset1: ";
std::copy(unordered_multiset1.begin(), unordered_multiset1.end(),
std::ostream_iterator<Cell>(std::cout, " "));
std::cout << std::endl;
std::cout << "unordered_multiset2: ";
std::copy(unordered_multiset2.begin(), unordered_multiset2.end(),
std::ostream_iterator<Cell>(std::cout, " "));
std::cout << std::endl;
return 0;
}
输出