std::hash
std::hash (std::filesystem::path 특수화)
std::hash의 std::filesystem::path 특수화예요. 경로 객체의 해시 값을 얻을 수 있게 해줘요. C++17부터 있어요.
출처: cppreference
본문
<filesystem> 헤더에 정의돼 있어요.
template<>
struct hash<std::filesystem::path>;
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 <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';
}