定義于頭檔案 <unordered_set>
template< class Key, class Hash = std::hash<Key>, class KeyEqual = std::equal_to<Key>, class Allocator = std::allocator<Key> > class unordered_set; | (1) | (C++11 起) |
namespace pmr { template <class Key, class Hash = std::hash<Key>, class Pred = std::equal_to<Key>> using unordered_set = std::unordered_set<Key, Hash, Pred, std::pmr::polymorphic_allocator<Key>>; } | (2) | (C++17 起) |
unordered_set is 是含有 Key 類型唯一對象集合的關聯容器。搜尋、插入和移除擁有平均常數時間複雜度。
在内部,元素并不以任何特别順序排序,而是組織進桶中。元素被放進哪個桶完全依賴其值的哈希。這允許對單獨元素的快速通路,因為哈希一旦,就準确指代元素被放入的桶。
不可修改容器元素(即使通過非 const 疊代器),因為修改可能更改元素的哈希,并破壞容器。
容量
檢查容器是否為空
std::unordered_set<Key,Hash,KeyEqual,Allocator>::empty
bool empty() const noexcept; | (C++11 起) (C++20 前) |
[[nodiscard]] bool empty() const noexcept; | (C++20 起) |
檢查容器是否無元素,即是否 begin() == end() 。
參數
(無)
傳回值
若容器為空則為 true ,否則為 false
複雜度
常數。
傳回容納的元素數
std::unordered_set<Key,Hash,KeyEqual,Allocator>::size
size_type size() const noexcept; | (C++11 起) |
傳回容器中的元素數,即 std::distance(begin(), end()) 。
參數
(無)
傳回值
容器中的元素數量。
複雜度
常數。
傳回可容納的最大元素數
std::unordered_set<Key,Hash,KeyEqual,Allocator>::max_size
size_type max_size() const noexcept; | (C++11 起) |
傳回根據系統或庫實作限制的容器可保有的元素最大數量,即對于最大容器的 std::distance(begin(), end()) 。
參數
(無)
傳回值
元素數量的最大值。
複雜度
常數。
注意
此值通常反映容器大小上的理論極限,至多為 std::numeric_limits<difference_type>::max() 。運作時,可用 RAM 總量可能會限制容器大小到小于
max_size()
的值。
調用示例
#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::vector<Cell> vector1(6);
std::generate(vector1.begin(), vector1.end(), generate);
std::cout << "vector1: ";
std::copy(vector1.begin(), vector1.end(), std::ostream_iterator<Cell>(std::cout, " "));
std::cout << std::endl;
//檢查容器是否無元素,即是否 begin() == end() 。
std::unordered_set<Cell, CHash, CEqual> unordered_set1;
std::cout << "unordered_set1 is empty " << unordered_set1.empty() << std::endl;
unordered_set1 = {generate(), generate(), generate(), generate(), generate(), generate()};;
std::cout << "unordered_set1: ";
std::copy(unordered_set1.begin(), unordered_set1.end(), std::ostream_iterator<Cell>(std::cout, " "));
std::cout << std::endl;
std::cout << "unordered_set1 is empty " << unordered_set1.empty() << std::endl;
std::cout << std::endl;
std::unordered_set<Cell, CHash, CEqual> unordered_set2;
for (size_t index = 0; index < 6; index++)
{
unordered_set2.emplace(std::rand() % 10 + 100, std::rand() % 10 + 100);
//傳回容器中的元素數,即 std::distance(begin(), end()) 。
std::cout << "unordered_set2 size: " << unordered_set2.size() << std::endl;
std::copy(unordered_set2.begin(), unordered_set2.end(), std::ostream_iterator<Cell>(std::cout, " "));
std::cout << std::endl;
}
std::cout << std::endl;
//傳回根據系統或庫實作限制的容器可保有的元素最大數量,
//即對于最大容器的 std::distance(begin(), end()) 。
std::unordered_set<char> unordered_set_c;
std::cout << "unordered_set<char> max_size: " << unordered_set_c.max_size() << std::endl;
std::unordered_set<int> unordered_set_i;
std::cout << "unordered_set<int> max_size: " << unordered_set_i.max_size() << std::endl;
std::unordered_set<uint8_t> unordered_set_ui8;
std::cout << "unordered_set<uint8_t> max_size: " << unordered_set_ui8.max_size() << std::endl;
std::unordered_set<uint16_t> unordered_set_ui16;
std::cout << "unordered_set<uint16_t> max_size: " << unordered_set_ui16.max_size() << std::endl;
std::unordered_set<uint32_t> unordered_set_ui32;
std::cout << "unordered_set<uint32_t> max_size: " << unordered_set_ui32.max_size() << std::endl;
std::unordered_set<uint64_t> unordered_set_ui64;
std::cout << "unordered_set<uint64_t> max_size: " << unordered_set_ui64.max_size() << std::endl;
std::unordered_set<short> unordered_set_s;
std::cout << "unordered_set<short> max_size: " << unordered_set_s.max_size() << std::endl;
std::unordered_set<double> unordered_set_d;
std::cout << "unordered_set<double> max_size: " << unordered_set_d.max_size() << std::endl;
std::unordered_set<float> unordered_set_f;
std::cout << "unordered_set<float> max_size: " << unordered_set_f.max_size() << std::endl;
std::unordered_set<long> unordered_set_l;
std::cout << "unordered_set<long> max_size: " << unordered_set_l.max_size() << std::endl;
std::unordered_set<long long> unordered_set_ll;
std::cout << "unordered_set<long long> max_size: " << unordered_set_ll.max_size() << std::endl;
std::unordered_set<string> unordered_set_str;
std::cout << "unordered_set<string> max_size: " << unordered_set_str.max_size() << std::endl;
std::unordered_set<Cell, CHash, CEqual> unordered_set_Cell;
std::cout << "unordered_set<Cell> max_size: " << unordered_set_Cell.max_size() << std::endl;
return 0;
}