std::hash<std::basic_string>
Min standard notice:
Header: <string>
The template specializations of std::hash for the various string classes allow users to obtain hashes of strings.
# Declarations
template< class A >
struct hash<std::basic_string<char, std::char_traits<char>, A>>;
(since C++11)
template< class A >
struct hash<std::basic_string<char16_t, std::char_traits<char16_t>, A>>;
(since C++11)
template< class A >
struct hash<std::basic_string<char32_t, std::char_traits<char32_t>, A>>;
(since C++11)
template< class A >
struct hash<std::basic_string<wchar_t, std::char_traits<wchar_t>, A>>;
(since C++11)
template< class A >
struct hash<std::basic_string<char8_t, std::char_traits<char8_t>, A>>;
(since C++20)
# Example
#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';
}
# Defect reports
| 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
- hash
- std::hashstd::string_viewstd::hashstd::wstring_viewstd::hashstd::u8string_viewstd::hashstd::u16string_viewstd::hashstd::u32string_view