hash_map的使用

例子:

#include <stdio.h>
#include <string>
#include <hash_map>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
	hash_map<int, int> hm;
	//插入元素
	for (int i = 0; i < 10; i++)
		hm.insert(make_pair(i, i));
	//正序输出
	hash_map<int, int>::iterator iter = hm.begin();
	for (; iter != hm.end(); ++iter)
		printf("hm[%d] = %d\n", iter->first, iter->second);
	printf("\n");
	//逆序输出
	hash_map<int, int>::reverse_iterator riter = hm.rbegin();
	for (; riter != hm.rend(); ++riter)
		printf("hm[%d] = %d\n", riter->first, riter->second);
	getchar();
	return 0;
}

输出结果为:

hm[0] = 0
hm[8] = 8
hm[1] = 1
hm[2] = 2
hm[3] = 3
hm[4] = 4
hm[5] = 5
hm[6] = 6
hm[7] = 7
hm[9] = 9

hm[9] = 9
hm[7] = 7
hm[6] = 6
hm[5] = 5
hm[4] = 4
hm[3] = 3
hm[2] = 2
hm[1] = 1
hm[8] = 8
hm[0] = 0

分析: 没什么好说的


例子:

#include <stdio.h>
#include <string>
#include <hash_map>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
	hash_map<const char*, int> s_hm;
	s_hm["a"] = 123;
	s_hm["b"] = 321;
	char str[60];
	scanf("%s", str);
	printf("s_hm[\"%s\"] = %d\n", str, s_hm[str]);
	getchar();
	getchar();
	return 0;
}

输出结果为:

a
s_hm["a"] = 0

分析: 输出结果与我们预期的不符,预期结果应该是输出 s_hm["a"] = 123, 这是为什么?

我们分析一下hash_map的源码,在vs2013跟进去调试后,可以看到这关键的一段代码

bool operator()(const _Ty& _Left, const _Ty& _Right) const
{	// apply operator< to operands
	return (_Left < _Right);
}

根据key算出hash值锁定某个桶后,我们会在这个桶中寻找元素,在同种寻找元素需要进行key的比较,我们输进去的key是char [] 型,hash_map里的key是const char*型,这两者用 "<" 去比较肯定是不相同,它比较字符串指针,并不比较字符串内容,所以找不到相应的元素


为了处理这种情况,我们可以模仿写一个比较函数

#include <stdio.h>
#include <string>
#include <hash_map>
using namespace std;

struct CharLess : public binary_function<const char*, const char*, bool>
{
	bool operator()(const first_argument_type& _Left, const second_argument_type& _Right)
	{
		return strcmp(_Left, _Right);
	}
};

int _tmain(int argc, _TCHAR* argv[])
{
	hash_map<const char*, int, hash_compare<const char*, CharLess> > s_hm;
	s_hm["a"] = 123;
	s_hm["b"] = 321;
	char str[60];
	scanf("%s", str);
	printf("s_hm[\"%s\"] = %d\n", str, s_hm[str]);
	getchar();
	getchar();
	return 0;
}

运行结果如下:

a
s_hm["a"] = 123


例子:

#include <stdio.h>
#include <string>
#include <hash_map>
using namespace std;

struct CharLess : public binary_function<const char*, const char*, bool>
{
	bool operator()(const first_argument_type& _Left, const second_argument_type& _Right)
	{
		return strcmp(_Left, _Right);
	}
};

int _tmain(int argc, _TCHAR* argv[])
{
	hash_map<const char*, int, hash_compare<const char*, CharLess> > s_hm;
	//获取桶个数
	printf("bucket_count: %d\n", s_hm.bucket_count());
	//获取最大桶个数
	printf("max_bucket_count: %d\n", s_hm.max_bucket_count());
	//_Bucket_size这个元素表示所有桶平均最多可以存几个元素,如果平均超过这个值,就需要进行rehash操作
	printf("_Bucket_size: %d\n", s_hm._Bucket_size);
	//bucket(key)计算对应key会放到哪个桶里
	printf("s_hm.bucket(\"%s\") : %d\n", "hello", s_hm.bucket("hello"));
	printf("s_hm.bucket(\"%s\") :%d\n", "helloa", s_hm.bucket("helloa"));
	printf("s_hm.bucket(\"%s\") :%d\n", "hellob", s_hm.bucket("hellob"));
	s_hm.insert(make_pair("hellob", 2));
	s_hm.insert(make_pair("hello", 1));
	s_hm.insert(make_pair("helloa", 1));

	for (int i = 0; i < s_hm.bucket_count(); i++)
		//bucket_size(i)获取第i个桶里存了的元素数量
		printf("bucket_size(%d): %d\n", i, s_hm.bucket_size(i));

	hash_map<const char*, int>::iterator iter = s_hm.begin();
	for (; iter != s_hm.end(); ++iter)
		printf("%s: %d\n", iter->first, iter->second);
	printf("\n");
	getchar();
	return 0;
}

输出结果为:

bucket_count: 8
max_bucket_count: 8
_Bucket_size: 1
s_hm.bucket("hello") : 3
s_hm.bucket("helloa") :2
s_hm.bucket("hellob") :2
bucket_size(0): 0
bucket_size(1): 0
bucket_size(2): 2
bucket_size(3): 1
bucket_size(4): 0
bucket_size(5): 0
bucket_size(6): 0
bucket_size(7): 0
helloa: 1
hellob: 2
hello: 1

分析:默认初始化的bucket_count和max_bucket_count都为8,_bucket_szie则是1


例子:

#include <stdio.h>
#include <string>
#include <hash_map>
using namespace std;

struct CharLess : public binary_function<const char*, const char*, bool>
{
	bool operator()(const first_argument_type& _Left, const second_argument_type& _Right)
	{
		return strcmp(_Left, _Right);
	}
};

int _tmain(int argc, _TCHAR* argv[])
{
	hash_map<const char*, int, hash_compare<const char*, CharLess> > s_hm;
	//获取桶个数
	printf("bucket_count: %d\n", s_hm.bucket_count());
	//获取最大桶个数
	printf("max_bucket_count: %d\n", s_hm.max_bucket_count());
	//_Bucket_size这个元素表示所有桶平均最多可以存几个元素,如果平均超过这个值,就需要进行rehash操作
	printf("_Bucket_size: %d\n", s_hm._Bucket_size);
	//bucket(key)计算对应key会放到哪个桶里
	printf("s_hm.bucket(\"%s\") : %d\n", "hello", s_hm.bucket("hello"));
	printf("s_hm.bucket(\"%s\") : %d\n", "helloa", s_hm.bucket("helloa"));
	printf("s_hm.bucket(\"%s\") : %d\n", "hellob", s_hm.bucket("hellob"));
	printf("s_hm.bucket(\"%s\") : %d\n", "hellol", s_hm.bucket("hellol"));
	printf("s_hm.bucket(\"%s\") : %d\n", "helloal", s_hm.bucket("helloal"));
	printf("s_hm.bucket(\"%s\") : %d\n", "hellobl", s_hm.bucket("hellobl"));
	printf("s_hm.bucket(\"%s\") : %d\n", "hellom", s_hm.bucket("hellom"));
	printf("s_hm.bucket(\"%s\") : %d\n", "helloam", s_hm.bucket("helloam"));
	printf("s_hm.bucket(\"%s\") : %d\n", "hellobm", s_hm.bucket("hellobm"));
	s_hm.insert(make_pair("hellob", 2));
	s_hm.insert(make_pair("hello", 1));
	s_hm.insert(make_pair("helloa", 1));;
	s_hm.insert(make_pair("hellobl", 2));
	s_hm.insert(make_pair("hellol", 1));
	s_hm.insert(make_pair("helloal", 1));
	s_hm.insert(make_pair("hellobm", 2));
	s_hm.insert(make_pair("hellom", 1));
	//s_hm.insert(make_pair("helloam", 1));
	for (int i = 0; i < s_hm.bucket_count(); i++)
	{
		//bucket_size(i)获取第i个桶里存了的元素数量
		if (s_hm.bucket_size(i) != 0)
			printf("bucket_size(%d): %d\n", i, s_hm.bucket_size(i));
	}
	//获取桶个数
	printf("bucket_count: %d\n", s_hm.bucket_count());
	//获取最大桶个数
	printf("max_bucket_count: %d\n", s_hm.max_bucket_count());
	hash_map<const char*, int>::iterator iter = s_hm.begin();
	for (; iter != s_hm.end(); ++iter)
		printf("%s: %d\n", iter->first, iter->second);
	printf("\n");
	getchar();
	return 0;
}
运行结果为:

bucket_count: 8
max_bucket_count: 8
_Bucket_size: 1
s_hm.bucket("hello") : 3
s_hm.bucket("helloa") : 2
s_hm.bucket("hellob") : 2
s_hm.bucket("hellol") : 1
s_hm.bucket("helloal") : 5
s_hm.bucket("hellobl") : 1
s_hm.bucket("hellom") : 1
s_hm.bucket("helloam") : 5
s_hm.bucket("hellobm") : 1
bucket_size(1): 4
bucket_size(2): 2
bucket_size(3): 1
bucket_size(5): 1
bucket_count: 8
max_bucket_count: 8
helloa: 1
hellob: 2
hello: 1
hellom: 1
hellobm: 2
hellol: 1
hellobl: 2
helloal: 1

#include <stdio.h>
#include <string>
#include <hash_map>
using namespace std;

struct CharLess : public binary_function<const char*, const char*, bool>
{
	bool operator()(const first_argument_type& _Left, const second_argument_type& _Right)
	{
		return strcmp(_Left, _Right);
	}
};

int _tmain(int argc, _TCHAR* argv[])
{
	hash_map<const char*, int, hash_compare<const char*, CharLess> > s_hm;
	//获取桶个数
	printf("bucket_count: %d\n", s_hm.bucket_count());
	//获取最大桶个数
	printf("max_bucket_count: %d\n", s_hm.max_bucket_count());
	//_Bucket_size这个元素表示所有桶平均最多可以存几个元素,如果平均超过这个值,就需要进行rehash操作
	printf("_Bucket_size: %d\n", s_hm._Bucket_size);
	//bucket(key)计算对应key会放到哪个桶里
	printf("s_hm.bucket(\"%s\") : %d\n", "hello", s_hm.bucket("hello"));
	printf("s_hm.bucket(\"%s\") : %d\n", "helloa", s_hm.bucket("helloa"));
	printf("s_hm.bucket(\"%s\") : %d\n", "hellob", s_hm.bucket("hellob"));
	printf("s_hm.bucket(\"%s\") : %d\n", "hellol", s_hm.bucket("hellol"));
	printf("s_hm.bucket(\"%s\") : %d\n", "helloal", s_hm.bucket("helloal"));
	printf("s_hm.bucket(\"%s\") : %d\n", "hellobl", s_hm.bucket("hellobl"));
	printf("s_hm.bucket(\"%s\") : %d\n", "hellom", s_hm.bucket("hellom"));
	printf("s_hm.bucket(\"%s\") : %d\n", "helloam", s_hm.bucket("helloam"));
	printf("s_hm.bucket(\"%s\") : %d\n", "hellobm", s_hm.bucket("hellobm"));
	s_hm.insert(make_pair("hellob", 2));
	s_hm.insert(make_pair("hello", 1));
	s_hm.insert(make_pair("helloa", 1));;
	s_hm.insert(make_pair("hellobl", 2));
	s_hm.insert(make_pair("hellol", 1));
	s_hm.insert(make_pair("helloal", 1));
	s_hm.insert(make_pair("hellobm", 2));
	s_hm.insert(make_pair("hellom", 1));
	s_hm.insert(make_pair("helloam", 1));
	for (int i = 0; i < s_hm.bucket_count(); i++)
	{
		//bucket_size(i)获取第i个桶里存了的元素数量
		if (s_hm.bucket_size(i) != 0)
			printf("bucket_size(%d): %d\n", i, s_hm.bucket_size(i));
	}
	//获取桶个数
	printf("bucket_count: %d\n", s_hm.bucket_count());
	//获取最大桶个数
	printf("max_bucket_count: %d\n", s_hm.max_bucket_count());
	hash_map<const char*, int>::iterator iter = s_hm.begin();
	for (; iter != s_hm.end(); ++iter)
		printf("%s: %d\n", iter->first, iter->second);
	printf("\n");
	getchar();
	return 0;
}

运行结果为:

s_hm.bucket("hellom") : 1
s_hm.bucket("helloam") : 5
s_hm.bucket("hellobm") : 1
bucket_size(2): 1
bucket_size(5): 1
bucket_size(9): 1
bucket_size(29): 1
bucket_size(33): 1
bucket_size(35): 1
bucket_size(42): 1
bucket_size(49): 1
bucket_size(57): 1
bucket_count: 64
max_bucket_count: 64
helloa: 1
hellob: 2
hello: 1
hellom: 1
hellobm: 2
hellol: 1
hellobl: 2
helloam: 1
helloal: 1

分析:当当如元素超过8个(超过原来bucket_count的值),为9个的时候,bucket_count和max_bucket_count都变大了,这时进行了一次rehash


#include <stdio.h>
#include <string>
#include <hash_map>
using namespace std;

struct CharLess : public binary_function<const char*, const char*, bool>
{
	bool operator()(const first_argument_type& _Left, const second_argument_type& _Right)
	{
		return strcmp(_Left, _Right);
	}
};

int _tmain(int argc, _TCHAR* argv[])
{
	hash_map<const char*, int, hash_compare<const char*, CharLess> > s_hm;
	//获取桶个数
	printf("bucket_count: %d\n", s_hm.bucket_count());
	//获取最大桶个数
	printf("max_bucket_count: %d\n", s_hm.max_bucket_count());
	//设置_Max_bucket_size的值
	s_hm.max_load_factor(2);
	//max_load_factor()获取_Max_bucket_size的值,这个元素表示所有桶平均最多可以存几个元素,如果平均超过这个值,就需要进行rehash操作
	printf("_Max_bucket_size: %f\n", s_hm.max_load_factor());
	//bucket(key)计算对应key会放到哪个桶里
	printf("s_hm.bucket(\"%s\") : %d\n", "hello", s_hm.bucket("hello"));
	printf("s_hm.bucket(\"%s\") : %d\n", "helloa", s_hm.bucket("helloa"));
	printf("s_hm.bucket(\"%s\") : %d\n", "hellob", s_hm.bucket("hellob"));
	printf("s_hm.bucket(\"%s\") : %d\n", "hellol", s_hm.bucket("hellol"));
	printf("s_hm.bucket(\"%s\") : %d\n", "helloal", s_hm.bucket("helloal"));
	printf("s_hm.bucket(\"%s\") : %d\n", "hellobl", s_hm.bucket("hellobl"));
	printf("s_hm.bucket(\"%s\") : %d\n", "hellom", s_hm.bucket("hellom"));
	printf("s_hm.bucket(\"%s\") : %d\n", "helloam", s_hm.bucket("helloam"));
	printf("s_hm.bucket(\"%s\") : %d\n", "hellobm", s_hm.bucket("hellobm"));
	s_hm.insert(make_pair("hellob", 2));
	s_hm.insert(make_pair("hello", 1));
	s_hm.insert(make_pair("helloa", 1));;
	s_hm.insert(make_pair("hellobl", 2));
	s_hm.insert(make_pair("hellol", 1));
	s_hm.insert(make_pair("helloal", 1));
	s_hm.insert(make_pair("hellobm", 2));
	s_hm.insert(make_pair("hellom", 1));
	s_hm.insert(make_pair("helloam", 1));
	for (int i = 0; i < s_hm.bucket_count(); i++)
	{
		//bucket_size(i)获取第i个桶里存了的元素数量

		if (s_hm.bucket_size(i) != 0)
			printf("bucket_size(%d): %d\n", i, s_hm.bucket_size(i));
	}
	//获取桶个数
	printf("bucket_count: %d\n", s_hm.bucket_count());
	//获取最大桶个数
	printf("max_bucket_count: %d\n", s_hm.max_bucket_count());
	hash_map<const char*, int>::iterator iter = s_hm.begin();
	for (; iter != s_hm.end(); ++iter)
		printf("%s: %d\n", iter->first, iter->second);
	printf("\n");
	getchar();
	return 0;
}

输出结果为:

bucket_count: 8
max_bucket_count: 8
_Max_bucket_size: 2.000000
s_hm.bucket("hello") : 3
s_hm.bucket("helloa") : 2
s_hm.bucket("hellob") : 2
s_hm.bucket("hellol") : 1
s_hm.bucket("helloal") : 5
s_hm.bucket("hellobl") : 1
s_hm.bucket("hellom") : 1
s_hm.bucket("helloam") : 5
s_hm.bucket("hellobm") : 1
bucket_size(1): 4
bucket_size(2): 2
bucket_size(3): 1
bucket_size(5): 2
bucket_count: 8
max_bucket_count: 8
helloa: 1
hellob: 2
hello: 1
hellom: 1
hellobm: 2
hellol: 1
hellobl: 2
helloam: 1
helloal: 1

分析:以上例子吧_Max_bucket_size设置为2后,插入9个元素后哈希表没有rehash操作,这时的临界值上升为16

注意:之前的例子有一个错误,_Bucket_size这个成员变量只是hash_map初始化时用来初始化_Max_bucket_size的, 上面有几个例子把它的作用标错了



reserve函数的使用

#include <stdio.h>
#include <string>
#include <hash_map>
using namespace std;

struct CharLess : public binary_function<const char*, const char*, bool>
{
	bool operator()(const first_argument_type& _Left, const second_argument_type& _Right)
	{
		return strcmp(_Left, _Right);
	}
};

int _tmain(int argc, _TCHAR* argv[])
{
	hash_map<const char*, int, hash_compare<const char*, CharLess> > s_hm;
	//获取桶个数
	printf("bucket_count: %d\n", s_hm.bucket_count());
	//获取最大桶个数
	printf("max_bucket_count: %d\n", s_hm.max_bucket_count());
	//设置_Max_bucket_size的值
	s_hm.max_load_factor(2);
	//max_load_factor()获取_Max_bucket_size的值,这个元素表示所有桶平均最多可以存几个元素,如果平均超过这个值,就需要进行rehash操作
	printf("_Max_bucket_size: %f\n", s_hm.max_load_factor());
	s_hm.reserve(100);
	//获取桶个数
	printf("bucket_count: %d\n", s_hm.bucket_count());
	//获取最大桶个数
	printf("max_bucket_count: %d\n", s_hm.max_bucket_count());
	getchar();
	return 0;
}

运行结果为

bucket_count: 8
max_bucket_count: 8
_Max_bucket_size: 2.000000
bucket_count: 64
max_bucket_count: 64


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值