本篇学习unordered_map的桶接口和哈希策略操作,具体的操作函数如下:
begin(size_type) cbegin(size_type) (C++11) | 返回一个迭代器,指向指定的桶的开始 (公开成员函数) |
end(size_type) cend(size_type) (C++11) | 返回一个迭代器,指向指定的桶的末尾 (公开成员函数) |
(C++11) | 返回桶数 (公开成员函数) |
(C++11) | 返回桶的最大数量 (公开成员函数) |
(C++11) | 返回在特定的桶中的元素数量 (公开成员函数) |
(C++11) | 返回带有特定键的桶 (公开成员函数) |
哈希策略 | |
(C++11) | 返回每个桶的平均元素数量 (公开成员函数) |
(C++11) | 管理每个桶的平均元素数量的最大值 (公开成员函数) |
(C++11) | 为至少为指定数量的桶预留存储空间并重新生成散列表 (公开成员函数) |
(C++11) | 为至少为指定数量的元素预留存储空间并重新生成哈希表。 (公开成员函数) |
示例代码:
#include <iostream>
#include <unordered_map>
#include <string>
using namespace std;
void bucketOperator()
{
unordered_map<int, string> map1 = {
{20, "apple"},{21, "pear"},{23, "peach"},{22, "apricot"},{24, "grape"},
{30, "cherry"},{31, "plum"},{33, "strawberry"},{32, "date"},{34, "watermelon"},
{10, "melon"},{11, "banana"},{13, "orange"},{12, "pineapple"},{14, "hawthorn"},
{29, "cherry"},{27, "cherry"},{28, "cherry"},{26, "cherry"},{25, "cherry"},
{39, "peach"},{37, "cherry"},{38, "cherry"},{36, "cherry"},{35, "cherry"},
{19, "melon"},{17, "cherry"},{18, "cherry"},{16, "cherry"},{15, "cherry"},
};
cout << "map1.size = " << map1.size() << " map1.empty = " << map1.empty() << " map1.max_size = " << map1.max_size() << endl;
cout << "map1的值为:" << endl;
int n = 0;
for(auto &val: map1)
{
cout << val.first << ": " << val.second << "\t";
++n;
if(n % 5 == 0)
cout << endl;
}
//1.返回桶数bucket_count
int bCount = map1.bucket_count();
cout << "bucket_count = " << bCount << endl;
//由运行结果看到一个元素一个桶,并用桶的个数比元素还多
//2.返回桶的最大数量max_bucket_count
int bMax = map1.max_bucket_count();
cout << "max_bucket_count = " << bMax << endl;
//3.返回在特定的桶中的元素数量bucket_size
std::unordered_map<std::string,std::string> map2 = {
{"longan","龙眼"},
{"loquat","枇杷"},
{"mandarin","橘子"},
{"megranate","石榴"}
};
for (auto& x: map2) {
std::cout << x.first << ":" << x.second;
std::cout << " is in bucket #" << map2.bucket (x.first) << std::endl;
}
int bc = map2.bucket_count();
cout << "map2.count == " << bc << endl;
for(int i = 0; i < bc; ++i)
{
int bs = map2.bucket_size(i);
cout << i << "号桶的元素个数 = " << bs << endl;
}
cout << endl;
//4.返回带有特定键的桶 的编号bucket
int nIndex = map1.bucket(90);//为什么不是关键字也能返回nIndex
cout << "bucket index = " << nIndex << endl;
//5.桶的开始迭代器begin(index), cbegin(index);
int index = 2;
int nCount = map1.bucket_size(index);//第index个桶元素的数量
if(nCount > 0)
{
unordered_map<int, string>::iterator beginIt = map1.begin(index);
cout << beginIt->first << "==>" << beginIt->second << endl;
//修改里面的值
beginIt->second = "camel";
unordered_map<int, string>::const_iterator cbeginIt = map1.cbegin(index);
cout << cbeginIt->first << "==>" << cbeginIt->second << endl;
//cbeginIt->second = "aaa";//常量迭代器不能修改值
}
else
{
cout << "这个桶是空的,没有元素" << endl;
}
//6.桶的尾迭代器end(index), cend(index);
if(nCount > 0)
{
unordered_map<int, string>::iterator endIt = map1.end(index);
--endIt;
cout << endIt->first << "==>" << endIt->second << endl;
//修改里面的值
endIt->second = "tiger";
unordered_map<int, string>::const_iterator cendIt = map1.cend(index);
--cendIt;
cout << cendIt->first << "==>" << cendIt->second << endl;
//cendIt->second = "bbb";//常量迭代器不能修改值
}
//7.load_factor(C++11)返回每个桶的平均元素数量
int loadFactor = map1.load_factor();
cout << "loadFactor = " << loadFactor << endl;
//8.max_load_factor(C++11)管理每个桶的平均元素数量的最大值
int maxLoadFactor = map1.max_load_factor();//最大因子为1,表示每个桶最多一个无素
cout << "maxLoadFactor = " << maxLoadFactor << endl;
//9.rehash(C++11)为至少为指定数量的桶预留存储空间并重新生成散列表
int b1 = map1.bucket_count();
cout << "原桶个数 ====== " << b1 << endl;
map1.rehash(32);//设置为32个桶 设置的这个值不能比元素个数少,如果设置的比元素个数少,则会设置成元素的个数
//例如,现在有32个元素,如果设置为16,设置后,桶的个数会是32
int b2 = map1.bucket_count();
cout << "设置后桶个数 == " << b2 << endl;
//10.reserve(C++11)为至少为指定数量的元素预留存储空间并重新生成哈希表。
int rs1 = map1.bucket_count();
cout << "原桶个数 ====== " << rs1 << endl;
map1.reserve(34);//设置为32个桶 分配的空间大于等于元素的个数,
//例如,设置为32,则桶个数为32,设置为34,桶的个数为64,空间成倍分配
int rs2 = map1.bucket_count();
cout << "重置后桶个数 == " << rs2 << endl;
}
int main()
{
bucketOperator();
cout << "hello world" << endl;
return 0;
}
运行结果:
参考:
https://zh.cppreference.com/w/cpp/container/unordered_map
http://www.cplusplus.com/reference/unordered_map/unordered_map/