std::hash <std::experimental::optional>
來自 cppreference.com
< 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) |
雜湊函式物件 (類模板) |