std::hash <std::experimental::optional>
来自 cppreference.cn
< cpp | experimental | optional
定义于头文件 <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) |
哈希函数对象 (类模板) |