std::filesystem::hash_value
來自 cppreference.com
< cpp | filesystem | path
定義於標頭檔案 <filesystem> |
||
std::size_t hash_value( const std::filesystem::path& p ) noexcept; |
(C++17 起) | |
目錄 |
[編輯] 引數
p | - | 一個 std::filesystem::path 物件 |
[編輯] 返回值
一個雜湊值,使得對於兩個路徑,若 p1 == p2,則 hash_value(p1) == hash_value(p2)。
返回值與 std::hash 一致。
[編輯] 注意
兩個路徑的相等性透過分別比較每個元件來確定,因此,例如 "a//b" 等於 "a/b",並且具有相同的 hash_value
。
hash_value
源於 Boost.filesystem 庫,其中它用於與 boost.hash 的互操作性(boost.hash 呼叫透過實參依賴查詢找到的 hash_value
,或在可用時呼叫 boost::hash_value
)。
[編輯] 示例
執行此程式碼
#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) << fs::hash_value(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); // A hash function object to work with unordered_* containers: struct PathHash { std::size_t operator()(fs::path const& p) const noexcept { return fs::hash_value(p); } }; std::unordered_set<fs::path, PathHash> 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++17)(C++17)(C++20 前)(C++17)(C++20 前)(C++17)(C++20 前)(C++17)(C++20 前)(C++17)(C++20 前)(C++20) |
按字典順序比較兩個路徑 (函式) |
(C++17) |
檢查兩個路徑是否引用相同的檔案系統物件 (函式) |
(C++11) |
雜湊函式物件 (類模板) |
對 std::filesystem::path 的雜湊支援 (類模板特化) |