std:: hash <std::string_view> , std:: hash <std::wstring_view> , std:: hash <std::u8string_view> , std:: hash <std::u16string_view> , std:: hash <std::u32string_view>
|
헤더에 정의됨
<string_view>
|
||
|
template
<>
struct
hash
<
std::
string_view
>
;
|
(C++17부터) | |
|
template
<>
struct
hash
<
std::
wstring_view
>
;
|
(C++17부터) | |
|
template
<>
struct
hash
<
std::
u8string_view
>
;
|
(C++20부터) | |
|
template
<>
struct
hash
<
std::
u16string_view
>
;
|
(C++17부터) | |
|
template
<>
struct
hash
<
std::
u32string_view
>
;
|
(C++17부터) | |
다양한 뷰 클래스에 대한 해시 뷰를 위한 std::hash 템플릿 특수화.
이 해시들은 해당 std::basic_string 클래스들의 해시와 동일합니다: S가 표준 basic_string 타입 중 하나이고, SV가 해당 string view 타입이며, s가 S 타입의 객체라면 std:: hash < S > ( ) ( s ) == std:: hash < SV > ( ) ( SV ( s ) ) .
예제
#include <iostream> #include <string_view> #include <unordered_set> using namespace std::literals; int main() { std::cout << "\"A\" #: " << std::hash<std::string_view>{}("A"sv) << '\n'; std::cout << "L\"B\" #: " << std::hash<std::wstring_view>{}(L"B"sv) << '\n'; std::cout << "u8\"C\" #: " << std::hash<std::u8string_view>{}(u8"C"sv) << '\n'; std::cout << "u\"D\" #: " << std::hash<std::u16string_view>{}(u"D"sv) << '\n'; std::cout << "U\"E\" #: " << std::hash<std::u32string_view>{}(U"E"sv) << '\n'; // std::hash for string_view family makes it possible to keep these view-types // in unordered_* associative containers, such as unordered_set. But ensure // the lifespan of referenced strings is no less than lifespan of the container, // i.e. no dangling references occurred. std::unordered_set stars{"Rigel"sv, "Capella"sv, "Vega"sv, "Arcturus"sv}; for (std::string_view const& s : stars) std::cout << s << ' '; std::cout << '\n'; }
가능한 출력:
"A" #: 6919333181322027406 L"B" #: 11959850520494268278 u8"C" #: 12432341034569643010 u"D" #: 312659256970442235 U"E" #: 18073225910249204957 Arcturus Vega Capella Rigel
참고 항목
|
(C++11)
|
해시 함수 객체
(클래스 템플릿) |
|
(C++11)
|
문자열을 위한 해시 지원
(클래스 템플릿 특수화) |