std::collate<CharT>:: hash, std::collate<CharT>:: do_hash
From cppreference.net
|
헤더 파일에 정의됨
<locale>
|
||
|
public
:
long hash ( const CharT * beg, const CharT * end ) const ; |
(1) | |
|
protected
:
virtual long do_hash ( const CharT * beg, const CharT * end ) const ; |
(2) | |
1)
Public 멤버 함수로서, 가장 파생된 클래스의 protected virtual 멤버 함수
do_hash
를 호출합니다.
2)
문자 시퀀스
[
beg
,
end
)
를 이 로캘에서 동등하게 배열되는 모든 문자열에 대해 얻은 해시와 동일한 정수 값으로 변환합니다(
compare()
가
0
을 반환함). 동등하게 배열되지 않는 두 문자열의 경우 해시가 동일할 확률은
1.0
/
std::
numeric_limits
<
unsigned
long
>
::
max
(
)
에 매우 가까울 정도로 매우 작아야 합니다.
목차 |
매개변수
| beg | - | 해시할 시퀀스의 첫 번째 문자를 가리키는 포인터 |
| end | - | 해시할 시퀀스의 끝 다음을 가리키는 포인터 |
반환값
콜레이션 순서를 존중하는 해시 값.
참고
시스템에서 제공하는 로케일은 일반적으로 두 문자열을 동등하게 정렬하지 않습니다( compare() 가 0 을 반환하지 않음) 만약 basic_string::operator== 가 false 를 반환하는 경우입니다. 하지만 사용자가 설치한 std::collate 패싯은 다른 정렬 규칙을 제공할 수 있으며, 예를 들어 문자열이 동일한 유니코드 정규화 형태를 가지는 경우 동등하게 처리할 수 있습니다.
예제
로케일을 인식하는 비정렬 컨테이너를 보여줍니다.
이 코드 실행
#include <iostream> #include <locale> #include <string> #include <unordered_set> struct CollateHash { template<typename CharT> long operator()(const std::basic_string<CharT>& s) const { return std::use_facet<std::collate<CharT>>(std::locale()).hash( &s[0], &s[0] + s.size() ); } }; struct CollateEq { template<typename CharT> bool operator()(const std::basic_string<CharT>& s1, const std::basic_string<CharT>& s2) const { return std::use_facet<std::collate<CharT>>(std::locale()).compare( &s1[0], &s1[0] + s1.size(), &s2[0], &s2[0] + s2.size() ) == 0; } }; int main() { std::locale::global(std::locale("en_US.utf8")); std::wcout.imbue(std::locale()); std::unordered_set<std::wstring, CollateHash, CollateEq> s2 = {L"Foo", L"Bar"}; for (auto& str : s2) std::wcout << str << ' '; std::cout << '\n'; }
가능한 출력:
Bar Foo
참고 항목
|
(C++11)
|
문자열을 위한 해시 지원
(클래스 템플릿 특수화) |