Skip site navigation (1)Skip section navigation (2)

FreeBSD Manual Pages

  
 
  

home | help
std::hash(3)		      C++ Standard Libary		  std::hash(3)

NAME
       std::hash - std::hash

Synopsis
	  Defined in header <filesystem>
	  template<>			       (since C++17)
	  struct hash<std::filesystem::path>;

	  The  template	 specialization	of std::hash for std::filesystem::path
       allows users to
	  obtain hash values of	std::filesystem::path.

	  The  operator()  of  this  specialization  is	 noexcept.  For	 every
       std::filesystem::path
	  value	p, std::hash<std::filesystem::path>{}(p) is equal to
	  std::filesystem::hash_value(p).

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

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::path	const& 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::path const& 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"

See also
	  hash	     hash function object
	  (C++11)    (class template)
	  hash_value calculates	a hash value for a path	object
	  (C++17)    (function)

http://cppreference.com		  2024.06.10			  std::hash(3)

Want to link to this manual page? Use this URL:
<https://man.freebsd.org/cgi/man.cgi?query=std::hash&manpath=FreeBSD+Ports+15.1.quarterly>

home | help