ref - http://cplusplus.com/reference/std/locale/collate/hash/
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.
[Example]
// 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;
}
No comments:
Post a Comment