Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

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

      From cppreference.com
      <cpp‎ |filesystem‎ |path
       
       
      Filesystem library
      Classes
      Functions
      File types
       
      std::filesystem::path
      Member types
      Member constants
      Member functions
      Path decomposition
      Non-member functions
      (until C++20)(until C++20)(until C++20)(until C++20)(until C++20)(C++20)
      Helper classes
      hash<std::filesystem::path>
       
      Defined in header<filesystem>
      template<>
      struct hash<std::filesystem::path>;
      (since C++17)

      The template specialization ofstd::hash forstd::filesystem::path allows users to obtain hash values ofstd::filesystem::path.

      Theoperator() of this specialization isnoexcept. For everystd::filesystem::path valuep,std::hash<std::filesystem::path>{}(p) is equal tostd::filesystem::hash_value(p).

      This specialization was absent from the C++17 standard publication, seeLWG issue 3657.

      [edit]Example

      Run this code
      #include <cassert>#include <cstddef>#include <filesystem>#include <iomanip>#include <iostream>#include <unordered_set>namespace fs= std::filesystem; void show_hash(fs::pathconst& 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::pathconst& p: dirs)std::cout<< p<<' ';std::cout<<'\n';}

      Possible output:

      6050C47ADB62DFE5 : "/tmp"62795A58B69AD90A : "/tmp/../tmp"FF302110C9991974 : "/a///b"FF302110C9991974 : "/a//b"FD6167277915D464 : "/a/c"C42040F82CD8B542 : "..."D2D30154E0B78BBC : ".."D18C722215ED0530 : "."               0 : """/tmp/../tmp" "/opt" "/lib" "/tmp" "/bin"

      [edit]See also

      (C++11)
      hash function object
      (class template)[edit]
      (C++17)
      calculates a hash value for a path object
      (function)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/filesystem/path/hash&oldid=172269"

      [8]ページ先頭

      ©2009-2025 Movatter.jp