Namespaces
Variants

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

From cppreference.net
定义于头文件 <experimental/optional>
template < class T >
struct hash < std:: experimental :: optional < T >> ;
(库基础技术规范)

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

模板参数

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

示例

#include <experimental/optional>
#include <iostream>
#include <string>
#include <unordered_set>
using namespace std::literals;
int main()
{
    // hash<optional> 使得可以使用 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)

参见

(C++11)
哈希函数对象
(类模板)