std::hash<std::filesystem::path>
From cppreference.com
< cpp | filesystem | path
| ヘッダー <filesystem> で定義 |
||
| template<> struct hash<std::filesystem::path>; |
(C++17以降) | |
std::hash の std::filesystem::path への特殊化は、ユーザーが std::filesystem::path のハッシュ値を取得することを可能にします。
この特殊化の operator() は noexcept です。std::filesystem::path のすべての値 p について、std::hash<std::filesystem::path>{}(p) は std::filesystem::hash_value(p) と等しくなります。
この特殊化はC++17標準公開時には存在しませんでした。 LWG issue 3657 を参照してください。
[編集] 例
このコードを実行
#include <cassert> #include <cstddef> #include <filesystem> #include <iomanip> #include <iostream> #include <unordered_set> namespace fs = std::filesystem; void show_hash(fs::path const& p) { std::cout << std::hex << std::uppercase << std::setw(16) << std::hash<fs::path>{}(p) << " : " << p << '\n'; } int main() { auto tmp1 = fs::path{"/tmp"}; auto tmp2 = fs::path{"/tmp/../tmp"}; assert(!(tmp1 == tmp2)); assert(fs::equivalent(tmp1, tmp2)); show_hash(tmp1); show_hash(tmp2); for (auto s : {"/a///b", "/a//b", "/a/c", "...", "..", ".", ""}) show_hash(s); std::unordered_set<fs::path, std::hash<fs::path>> dirs{ "/bin", "/bin", "/lib", "/lib", "/opt", "/opt", "/tmp", "/tmp/../tmp"}; for (fs::path const& p : dirs) std::cout << p << ' '; std::cout << '\n'; }
実行結果の例
6050C47ADB62DFE5 : "/tmp"
62795A58B69AD90A : "/tmp/../tmp"
FF302110C9991974 : "/a///b"
FF302110C9991974 : "/a//b"
FD6167277915D464 : "/a/c"
C42040F82CD8B542 : "..."
D2D30154E0B78BBC : ".."
D18C722215ED0530 : "."
0 : ""
"/tmp/../tmp" "/opt" "/lib" "/tmp" "/bin"[編集] 関連項目
| (C++11) |
ハッシュ関数オブジェクト (クラステンプレート) |
| (C++17) |
パスオブジェクトのハッシュ値を計算する (関数) |