Namespaces
Variants

std::filesystem:: hash_value

From cppreference.net
헤더 파일에 정의됨 <filesystem>
std:: size_t hash_value ( const std:: filesystem :: path & p ) noexcept ;
(C++17부터)

목차

매개변수

p - a 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);
    // unordered_* 컨테이너와 함께 사용할 해시 함수 객체:
    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"

참고 항목

두 경로의 어휘 표현을 사전식으로 비교합니다
(public member function)
(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)
두 경로를 사전식으로 비교합니다
(function)
(C++17)
두 경로가 동일한 파일 시스템 객체를 참조하는지 확인합니다
(function)
(C++11)
해시 함수 객체
(class template)
std::filesystem::path 에 대한 해시 지원
(class template specialization)