定义于头文件 <unordered_set>
template< class Key, | (1) | (C++11 起) |
namespace pmr { template <class Key, | (2) | (C++17 起) |
unordered_set is 是含有 Key 类型唯一对象集合的关联容器。搜索、插入和移除拥有平均常数时间复杂度。
在内部,元素并不以任何特别顺序排序,而是组织进桶中。元素被放进哪个桶完全依赖其值的哈希。这允许对单独元素的快速访问,因为哈希一旦,就准确指代元素被放入的桶。
不可修改容器元素(即使通过非 const 迭代器),因为修改可能更改元素的哈希,并破坏容器。
桶接口
返回桶数
std::unordered_set<Key,Hash,KeyEqual,Allocator>::bucket_count
size_type bucket_count() const; | (C++11 起) |
返回容器中的桶数。
参数
(无)
返回值
容器中的桶数。
复杂度
常数。
返回桶的最大数量
std::unordered_set<Key,Hash,KeyEqual,Allocator>::max_bucket_count
size_type max_bucket_count() const; | (C++11 起) |
返回容器由于系统或库实现限制的能保有的最大桶数。
参数
(无)
返回值
最大桶数。
复杂度
常数。
返回在特定的桶中的元素数量
std::unordered_set<Key,Hash,KeyEqual,Allocator>::bucket_size
size_type bucket_size( size_type n ) const; | (C++11 起) |
返回下标为 n
的桶中的元素数。
参数
n | - | 要检验的桶的下标 |
返回值
桶 n
中的元素数。
复杂度
与桶 n
的大小成线性。
返回带有特定键的桶
std::unordered_set<Key,Hash,KeyEqual,Allocator>::bucket
size_type bucket( const Key& key ) const; | (C++11 起) |
返回关键 key
的桶的下标。始终会在此桶中找到关键等于 key
的元素(若存在)。返回值仅对 bucket_count() 返回相同值的容器实例合法。
若 bucket_count() 为零则行为未定义。
参数
key | - | 要检验的关键值 |
返回值
关键 key
的桶编号。
复杂度
常数。
调用示例
#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 + 110;
Cell cell{n, n};
return cell;
};
std::unordered_set<Cell, CHash, CEqual> unordered_set1;
//6) 插入来自 initializer_list ilist 的元素。若范围中的多个元素拥有比较等价的关键,则插入哪个元素是未指定的
while (unordered_set1.size() < 6)
{
unordered_set1.insert({generate()});
//返回关键 key 的桶的下标。始终会在此桶中找到关键等于 key 的元素(若存在)。
//返回值仅对 bucket_count() 返回相同值的容器实例合法。
std::cout << "unordered_set1 bucket_count: " << unordered_set1.bucket_count() << std::endl;
}
std::cout << std::endl;
std::cout << "unordered_set1: ";
std::copy(unordered_set1.begin(), unordered_set1.end(), std::ostream_iterator<Cell>(std::cout, " "));
std::cout << std::endl;
std::cout << std::endl;
for (std::unordered_set<Cell, CHash, CEqual>::const_iterator cit =
unordered_set1.cbegin(); cit != unordered_set1.end(); cit++)
{
//返回关键 key 的桶的下标。始终会在此桶中找到关键等于 key 的元素(若存在)。
//返回值仅对 bucket_count() 返回相同值的容器实例合法。
size_t bucket = unordered_set1.bucket(*cit);
std::cout << "unordered_set1 bucket(" << *cit << ") : "
<< bucket << " ";
//返回关键 key 的桶的下标。始终会在此桶中找到关键等于 key 的元素(若存在)。
//返回值仅对 bucket_count() 返回相同值的容器实例合法。
size_t bucket_size = unordered_set1.bucket_size(bucket);
std::cout << "bucket_size(" << bucket << "): " << bucket_size << std::endl;
}
std::cout << std::endl;
//返回容器由于系统或库实现限制的能保有的最大桶数。
std::unordered_set<char> unordered_set_c;
std::cout << "unordered_set<char> max_bucket_count: " << unordered_set_c.max_bucket_count() << std::endl;
std::unordered_set<int> unordered_set_i;
std::cout << "unordered_set<int> max_bucket_count: " << unordered_set_i.max_bucket_count() << std::endl;
std::unordered_set<uint8_t> unordered_set_ui8;
std::cout << "unordered_set<uint8_t> max_bucket_count: " << unordered_set_ui8.max_bucket_count() << std::endl;
std::unordered_set<uint16_t> unordered_set_ui16;
std::cout << "unordered_set<uint16_t> max_bucket_count: " << unordered_set_ui16.max_bucket_count() << std::endl;
std::unordered_set<uint32_t> unordered_set_ui32;
std::cout << "unordered_set<uint32_t> max_bucket_count: " << unordered_set_ui32.max_bucket_count() << std::endl;
std::unordered_set<uint64_t> unordered_set_ui64;
std::cout << "unordered_set<uint64_t> max_bucket_count: " << unordered_set_ui64.max_bucket_count() << std::endl;
std::unordered_set<short> unordered_set_s;
std::cout << "unordered_set<short> max_bucket_count: " << unordered_set_s.max_bucket_count() << std::endl;
std::unordered_set<double> unordered_set_d;
std::cout << "unordered_set<double> max_bucket_count: " << unordered_set_d.max_bucket_count() << std::endl;
std::unordered_set<float> unordered_set_f;
std::cout << "unordered_set<float> max_bucket_count: " << unordered_set_f.max_bucket_count() << std::endl;
std::unordered_set<long> unordered_set_l;
std::cout << "unordered_set<long> max_bucket_count: " << unordered_set_l.max_bucket_count() << std::endl;
std::unordered_set<long long> unordered_set_ll;
std::cout << "unordered_set<long long> max_bucket_count: " << unordered_set_ll.max_bucket_count() << std::endl;
std::unordered_set<string> unordered_set_str;
std::cout << "unordered_set<string> max_bucket_count: " << unordered_set_str.max_bucket_count() << std::endl;
std::unordered_set<Cell, CHash, CEqual> unordered_set_Cell;
std::cout << "std::unordered_set<Cell, CHash, CEqual> max_bucket_count: " << unordered_set_Cell.max_bucket_count() << std::endl;
return 0;
}
输出