std::filesystem:: hash_value
|
定义于头文件
<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 的互操作性(后者通过
实参依赖查找
找到
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); // 用于无序容器的哈希函数对象: 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)
(until C++20)
(C++17)
(until C++20)
(C++17)
(until C++20)
(C++17)
(until C++20)
(C++17)
(until C++20)
(C++20)
|
按字典序比较两个路径
(函数) |
|
(C++17)
|
检查两个路径是否指向同一文件系统对象
(函数) |
|
(C++11)
|
哈希函数对象
(类模板) |
|
对
std::filesystem::path
的哈希支持
(类模板特化) |