On this page
std::hash<std::basic_string>
Defined in header <string> |
||
---|---|---|
|
(1) | (since C++11) |
|
(2) | (since C++11) |
|
(3) | (since C++11) |
|
(4) | (since C++11) |
|
(5) | (since C++20) |
The template specializations of std::hash
for the various string classes allow users to obtain hashes of strings.
These hashes equal the hashes of corresponding |
(since C++17) |
Example
The following code shows one possible output of a hash function used on a string:
#include <functional>
#include <iostream>
#include <memory_resource>
#include <string>
#include <string_view>
using namespace std::literals;
int main()
{
auto sv = "Stand back! I've got jimmies!"sv;
std::string s(sv);
std::pmr::string pmrs(sv); // use default allocator
std::cout << std::hash<std::string_view>{}(sv) << '\n';
std::cout << std::hash<std::string>{}(s) << '\n';
std::cout << std::hash<std::pmr::string>{}(pmrs) << '\n';
}
Possible output:
3544599705012401047
3544599705012401047
3544599705012401047
Defect reports
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR | Applied to | Behavior as published | Correct behavior |
---|---|---|---|
LWG 3705 | C++11 | hash support for std::basic_string with customized allocators was not enabled |
enabled |
See also
(C++11)
|
hash function object (class template) |
(C++17)(C++17)(C++20)(C++17)(C++17)
|
hash support for string views (class template specialization) |
© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
https://en.cppreference.com/w/cpp/string/basic_string/hash