std::hash <std::experimental::optional>
来自 cppreference.cn
< cpp | experimental | optional
定义于头文件 <experimental/optional> |
||
template< class T > struct hash<std::experimental::optional<T>>; |
(库基础 TS) | |
对 std::experimental::optional 类的 std::hash 的模板特化允许用户获取 `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> 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)
[编辑] 参阅
(C++11) |
哈希函数对象 (类模板) |