Reference
C Library
IOstream Library
Strings library
STL Containers
STL Algorithms
Miscellaneous
Miscellaneous
complex
exception
functional
iterator
limits
locale
memory
new
numeric
stdexcept
typeinfo
utility
valarray
locale
has_facet
isalnum
isalpha
iscntrl
isdigit
isgraph
islower
isprint
ispunct
isspace
isupper
isxdigit
locale
tolower
toupper
use_facet
standard facets:
codecvt
codecvt_base
codecvt_byname
collate
collate_byname
ctype
ctype_base
ctype_byname
messages
messages_base
messages_byname
moneypunct
moneypunct_byname
money_base
money_get
money_put
numpunct
numpunct_byname
num_get
num_put
time_base
time_get
time_get_byname
time_put
time_put_byname
collate
collate::collate
public member functions:
collate::compare
collate::hash
collate::transform
public member types:
collate::char_type
collate::string_type
protected members:
collate::do_compare
collate::do_hash
collate::do_transform
collate::~collate


collate::hash

public member function
long hash (const charT* low, const charT* high) const;

Get hash value

Returns the hash value of the string corresponding to the character sequence [low,high).

A hash for a string is a value that uniquely* identifies the content of the string, so that two strings with the same hash value, would compare equal by collate::compare, and two strings with different hash values would compare not equal.

Thus, two strings can be easily compared for equality by simply comparing their hash values, which are of an integer type.

During its operation, the version of this function in the generic template simply calls the virtual protected member do_transform, which is the member function in charge of performing the actions described above.

* The uniqueness is relative to the possible values the hash type can take (in this case numeric_limits<unsigned long>::max()). Therefore, there is a very small probability that two strings with different contents have the same hash (generally, one in billions).

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.
charT is the template parameter (i.e., the facet's character type).


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>
#include <locale>
using namespace std;
int main ()
{
  string myberry = "strawberry";
  string yourberry;
  locale loc;                 // the "C" locale
  const collate<char>& coll = use_facet<collate<char> >(loc);
  long myhash = coll.hash(myberry.data(),myberry.data()+myberry.length());
  cout << "Please, enter your favorite berry:";
  getline (cin,yourberry);
  long yourhash = coll.hash(yourberry.data(),yourberry.data()+yourberry.length());
  if (myhash == yourhash)
    cout << "Mine too!\n";
  else
    cout << "I prefer strawberries...\n";
  return 0;
}


Possible output:

Please enter your favorite berry: strawberry
Mine too!

See also