hash_map的使用

来源:互联网 发布:局域网是什么端口号 编辑:程序博客网 时间:2024/06/03 13:40

例子:

#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] = 0hm[8] = 8hm[1] = 1hm[2] = 2hm[3] = 3hm[4] = 4hm[5] = 5hm[6] = 6hm[7] = 7hm[9] = 9hm[9] = 9hm[7] = 7hm[6] = 6hm[5] = 5hm[4] = 4hm[3] = 3hm[2] = 2hm[1] = 1hm[8] = 8hm[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;}

输出结果为:

as_hm["a"] = 0

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

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

bool operator()(const _Ty& _Left, const _Ty& _Right) const{// apply operator< to operandsreturn (_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;}

运行结果如下:

as_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: 8max_bucket_count: 8_Bucket_size: 1s_hm.bucket("hello") : 3s_hm.bucket("helloa") :2s_hm.bucket("hellob") :2bucket_size(0): 0bucket_size(1): 0bucket_size(2): 2bucket_size(3): 1bucket_size(4): 0bucket_size(5): 0bucket_size(6): 0bucket_size(7): 0helloa: 1hellob: 2hello: 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: 8max_bucket_count: 8_Bucket_size: 1s_hm.bucket("hello") : 3s_hm.bucket("helloa") : 2s_hm.bucket("hellob") : 2s_hm.bucket("hellol") : 1s_hm.bucket("helloal") : 5s_hm.bucket("hellobl") : 1s_hm.bucket("hellom") : 1s_hm.bucket("helloam") : 5s_hm.bucket("hellobm") : 1bucket_size(1): 4bucket_size(2): 2bucket_size(3): 1bucket_size(5): 1bucket_count: 8max_bucket_count: 8helloa: 1hellob: 2hello: 1hellom: 1hellobm: 2hellol: 1hellobl: 2helloal: 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") : 1s_hm.bucket("helloam") : 5s_hm.bucket("hellobm") : 1bucket_size(2): 1bucket_size(5): 1bucket_size(9): 1bucket_size(29): 1bucket_size(33): 1bucket_size(35): 1bucket_size(42): 1bucket_size(49): 1bucket_size(57): 1bucket_count: 64max_bucket_count: 64helloa: 1hellob: 2hello: 1hellom: 1hellobm: 2hellol: 1hellobl: 2helloam: 1helloal: 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: 8max_bucket_count: 8_Max_bucket_size: 2.000000s_hm.bucket("hello") : 3s_hm.bucket("helloa") : 2s_hm.bucket("hellob") : 2s_hm.bucket("hellol") : 1s_hm.bucket("helloal") : 5s_hm.bucket("hellobl") : 1s_hm.bucket("hellom") : 1s_hm.bucket("helloam") : 5s_hm.bucket("hellobm") : 1bucket_size(1): 4bucket_size(2): 2bucket_size(3): 1bucket_size(5): 2bucket_count: 8max_bucket_count: 8helloa: 1hellob: 2hello: 1hellom: 1hellobm: 2hellol: 1hellobl: 2helloam: 1helloal: 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: 8max_bucket_count: 8_Max_bucket_size: 2.000000bucket_count: 64max_bucket_count: 64


0 0
原创粉丝点击