c++ hash函数

std::hash

template struct hash;


Unary function object class that defines the default hash function used by the standard library.

一元函数

Member functions

operator()
Returns a hash value for its argument, as a value of type  size_t.
#include 
#include 
#include 
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
	char nts1[] = "Test";
	char nts2[] = "Test";
	string str1 (nts1);
	string str2 (nts2);

	hash ptr_hash;
	hash str_hash;

	cout<<"hash value of nts1: "<	cout<<"hash value of nts2: "<	cout<<"hash value of str1: "<	cout<<"hash value of str2: "<	cout << "same hashes:\n" << boolalpha;
	cout << "nts1 and nts2: " << (ptr_hash(nts1)==ptr_hash(nts2)) << '\n';
	cout << "str1 and str2: " << (str_hash(str1)==str_hash(str2)) << '\n';

	return 0;
}

输出是:
hash value of nts1: 2071187971
hash value of nts2: 1344341107
hash value of str1: 805092869
hash value of str2: 805092869
same hashes:
nts1 and nts2: false
str1 and str2: true

你可能感兴趣的:(c++ hash函数)