std:: hash <std::vector<bool>>
From cppreference.net
<
cpp
|
container
|
vector bool
C++
Containers library
|
(C++17)
|
||||
| Sequence | ||||
|
(C++11)
|
||||
|
(C++26)
|
||||
|
(C++26)
|
||||
|
(C++11)
|
||||
| Associative | ||||
| Unordered associative | ||||
|
(C++11)
|
||||
|
(C++11)
|
||||
|
(C++11)
|
||||
|
(C++11)
|
||||
| Adaptors | ||||
|
(C++23)
|
||||
|
(C++23)
|
||||
|
(C++23)
|
||||
|
(C++23)
|
||||
| Views | ||||
|
(C++20)
|
||||
|
(C++23)
|
||||
| Tables | ||||
| Iterator invalidation | ||||
| Member function table | ||||
| Non-member function table |
std::vector<bool>
| Member types | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Member functions | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Non-member functions | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Helper classes | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
hash
<std::vector<bool>>
(C++11)
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Deduction guides (C++17) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
定义于头文件
<vector>
|
||
|
template
<
class
Allocator
>
struct hash < std:: vector < bool , Allocator >> ; |
(C++11 起) | |
针对 std:: vector < bool > 的 std::hash 模板特化允许用户获取 std:: vector < bool > 类型对象的哈希值。
示例
运行此代码
#include <iostream> #include <unordered_set> #include <vector> using vb = std::vector<bool>; vb to_vector_bool(unsigned n) { vb v; do { v.push_back(n & 1); n >>= 1; } while (n); return v; } auto print(const vb& v, bool new_line = true) { for (std::cout << "{ "; const bool e : v) std::cout << e << ' '; std::cout << '}' << (new_line ? '\n' : ' '); } int main() { for (auto i{0U}; i != 8; ++i) { std::cout << std::hex << std::uppercase; vb v = to_vector_bool(i); std::cout << std::hash<vb>{}(v) << ' ' << std::dec; print(v); } // std::hash for vector<bool> makes it possible to keep them in // unordered_* associative containers, such as unordered_set. std::unordered_set v{vb{0}, vb{0, 0}, vb{1}, vb{1}, vb{1, 0}, vb{1, 1}}; for (vb const& e : v) print(e, 0); std::cout << '\n'; }
可能的输出:
6D09EE26D5863619 { 0 }
3C27D9F591D20E49 { 1 }
E74D3F72B7599C63 { 0 1 }
EE3BE81F55123770 { 1 1 }
3AAD2A2EDBEC6C35 { 0 0 1 }
EB057F773CB64C43 { 1 0 1 }
6E1354730102BE00 { 0 1 1 }
E2E622597C18899D { 1 1 1 }
{ 1 1 } { 1 0 } { 1 } { 0 0 } { 0 }
参见
|
(C++11)
|
哈希函数对象
(类模板) |