Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::filesystem::hash_value

      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
       
      Defined in header<filesystem>
      std::size_t hash_value(conststd::filesystem::path& p)noexcept;
      (since C++17)

      Contents

      [edit]Parameters

      p - astd::filesystem::path object

      [edit]Return value

      A hash value such that if for two paths,p1== p2 thenhash_value(p1)== hash_value(p2).

      The return value is consistent withstd::hash.

      [edit]Notes

      Equality of two paths is determined by comparing each component separately, so, for example"a//b" equals"a/b" and has the samehash_value.

      hash_value originates from theBoost.filesystem library where it was used for interoperability with boost.hash (which callshash_value found byargument-dependent lookup orboost::hash_value where available).

      [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)<< 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); // A hash function object to work with unordered_* containers:struct PathHash{std::size_t operator()(fs::pathconst& p)constnoexcept{return fs::hash_value(p);}};std::unordered_set<fs::path, PathHash> 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

      compares the lexical representations of two paths lexicographically
      (public member function)[edit]
      (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)
      lexicographically compares two paths
      (function)[edit]
      (C++17)
      checks whether two paths refer to the same file system object
      (function)[edit]
      (C++11)
      hash function object
      (class template)[edit]
      hash support forstd::filesystem::path
      (class template specialization)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/filesystem/path/hash_value&oldid=158021"

      [8]ページ先頭

      ©2009-2025 Movatter.jp