Namespaces
Variants

std:: hash <std::filesystem::path>

From cppreference.net
헤더 파일에 정의됨 <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 이슈 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)
경로 객체에 대한 해시 값을 계산함
(함수)