std::hash

std::hash (std::filesystem::path 특수화)

std::hashstd::filesystem::path 특수화예요. 경로 객체의 해시 값을 얻을 수 있게 해줘요. C++17부터 있어요.

출처: cppreference

본문

<filesystem> 헤더에 정의돼 있어요.

template<>
struct hash<std::filesystem::path>;

std::hashstd::filesystem::path 특수화는 사용자가 std::filesystem::path의 해시 값을 얻을 수 있게 해줘요. 이 특수화의 operator()noexcept예요. 모든 std::filesystem::pathp에 대해 std::hash<std::filesystem::path>{}(p)std::filesystem::hash_value(p)와 같아요. 이 특수화는 C++17 표준 발행본에는 없었고, LWG 이슈 3657을 참조해요.

예제

#include <cassert>
#include <cstddef>
#include <filesystem>
#include <iostream>
#include <unordered_set>
namespace fs = std::filesystem;

int main()
{
    fs::path p1("a/b/c");
    // 해시 기반 컨테이너에 경로 사용 가능
    std::unordered_set<fs::path> s{fs::path("x"), fs::path("y")};
    std::cout << std::hash<fs::path>{}(p1) << '\n';
}

더 알아보기 (Learn more)

cppreference