命名空间
变体
操作

std::hash <std::experimental::optional>

来自 cppreference.cn
 
 
实验性
技术规范
文件系统库 (filesystem TS)
库基础 (library fundamentals TS)
库基础 2 (library fundamentals TS v2)
库基础 3 (library fundamentals TS v3)
并行性扩展 (parallelism TS)
并行性扩展 2 (parallelism TS v2)
并发性扩展 (concurrency TS)
并发性扩展 2 (concurrency TS v2)
概念 (concepts TS)
范围 (ranges TS)
反射 (reflection TS)
数学特殊函数 (special functions TR)
实验性非TS
模式匹配
线性代数
std::execution
合约
2D 图形
 
 
 
定义于头文件 <experimental/optional>
template< class T >
struct hash<std::experimental::optional<T>>;
(library fundamentals TS)

std::experimental::optional 类的 std::hash 模板特化允许用户获取 optional 对象中所含值的哈希值。

[edit] 模板参数

T - optional 对象中所含值的类型。特化 std::hash<T> 必须满足类模板 hash 的要求。

[edit] 示例

#include <experimental/optional>
#include <iostream>
#include <string>
#include <unordered_set>
using namespace std::literals;
 
int main()
{
    // hash<optional> makes it possible to use unordered_set
    std::unordered_set<std::experimental::optional<std::string>> s = {
        "abc"s, std::experimental::nullopt, "def"s
    };
 
    for (const auto& o : s)
        std::cout << o.value_or("(null)") << ' ';
    std::cout << '\n';
}

可能的输出

def abc (null)

[edit] 参见

(C++11)
哈希函数对象
(类模板) [编辑]