public member function
<locale>

std::collate::hash

long hash (const char_type* low, const char_type* high) const;
Get hash value
Returns a hash value for the string corresponding to the character sequence [low,high).

The hash value is such that two strings with the same hash value compare equal using collate::compare.

Note that the hash value is not guaranteed to be unique, therefore two different strings may end up producing the same has value. But the probability of such collisions shall approach 1/numeric_limits<unsigned long>::max() (one in billions on most systems).

Internally, this function simply calls the virtual protected member do_hash, which does the above by default.

Parameters

low, high
Pointers to the beginning and ending characters of the sequence. The range used is [low,high), which contains all the characters between low and high, including the character pointed by low but not the character pointed by high.
Note that null characters (if any) are also considered for the hash value as well as all characters beyond them, up until high.
Member type char_type is the facet's character type (defined as an alias of collate's template parameter, charT).

Return value

An integer value that identifies the content of the entire character sequence.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// collate::hash example
#include <iostream>       // std::cin, std::cout
#include <string>         // std::string, std::getline
#include <locale>         // std::locale, std::collate, std::use_facet

int main ()
{
  std::string myberry = "strawberry";
  std::string yourberry;

  std::locale loc;                 // the "C" locale

  const std::collate<char>& coll = std::use_facet<std::collate<char> >(loc);

  long myhash = coll.hash(myberry.data(),myberry.data()+myberry.length());

  std::cout << "Please, enter your favorite berry: ";
  std::getline (std::cin,yourberry);

  long yourhash = coll.hash(yourberry.data(),yourberry.data()+yourberry.length());

  if (myhash == yourhash)
    std::cout << "Mine too!\n";
  else
    std::cout << "I prefer strawberries...\n";

  return 0;
}

Possible output:

Please enter your favorite berry: strawberry
Mine too!


Data races

The facet object and the characters in the range [low,high) are accessed.

Exception safety

Strong guarantee: No side effects in case an exception is thrown.

See also