C++ STL中的unordered_map reserve()
我们知道,桶是容器内部哈希表中的一个槽,所有元素都基于它们的键的哈希值分配到其中。桶从0到bucket_count编号。现在,由于一个桶保持可变数量的项。这个数值基于一个术语 负载因子 。当 负载因子(load_factor) 达到某个阈值时,容器增加桶的数量并重新哈希地图。但是当我们调用 rehash(n) 时,它直接将桶的数量设置为n,并触发整个哈希表的重建。但是,当我们调用 reserve(n) 时,它创建足够的桶来至少容纳n个项目。如果然后我们在映射中添加>n个项目,取决于负载因子,可能会触发重新哈希。通过使用预期的unordered_map容器大小来调用reserve,我们避免了容器大小增加可能产生的多次重新哈希,并优化了哈希表的大小。C++函数std::unordered_map::reserve()将容器(bucket_count)中的桶的数量设置为最适合包含至少n个元素。
语法:
unordered_map_name.reserve(N)
参数: 该函数接受一个强制性参数 N ,指定请求的元素数为最小容量。
返回值: 该函数不返回任何内容。
下面的程序说明以上功能:
程序1:
// C++ program to illustrate the
// unordered_map::reserve()
#include <bits/stdc++.h>
using namespace std;
int main()
{
// declaration
unordered_map<int, int> sample1, sample2;
// the sample1 size is reserved for
// the bucket to contain a minimum of
// one elements
sample1.reserve(1);
// inserts key and element
// in sample1
sample1.insert({ 10, 100 });
sample1.insert({ 50, 500 });
// inserts key and element
// in sample1
// the sample1 size is reserved for
// the bucket to contain a minimum of
// three elements
sample2.reserve(3);
sample2.insert({ 20, 200 });
sample2.insert({ 30, 300 });
sample2.insert({ 30, 150 });
cout << "The size of Sample1 is: " << sample1.size();
cout << "\nKey and Elements of Sample1 are:";
for (auto it = sample1.begin(); it != sample1.end(); it++) {
cout << "{" << it->first << ", " << it->second << "} ";
}
cout << "\n\nThe size of Sample2 is: " << sample2.size();
cout << "\nKey and Elements of Sample2 are:";
for (auto it = sample2.begin(); it != sample2.end(); it++) {
cout << "{" << it->first << ", " << it->second << "} ";
}
return 0;
}
The size of Sample1 is: 2
Key and Elements of Sample1 are:{50,500} {10, 100}
The size of Sample2 is: 2
Key and Elements of Sample2 are:{30, 300} {20, 200}
程序2:
// C++ program to demonstrate the
// use of unordered_map::reserve()
#include <bits/stdc++.h>
using namespace std;
int main()
{
// Declaration of unordered_map
unordered_map<string, string> umap;
// Initialisation of unordered_map
umap = { { "An", "Apple" }, { "Ball", "Cat" },
{ "Cat", "Dog" }, { "Elephant", "Fish" },
{ "Dog", "Goat" } };
// displaying the original elements
cout << "Original unordered_map::"
<< " \n Key \t Value \n";
for (auto pair : umap) {
cout << " " << pair.first
<< " \t " << pair.second
<< " \n ";
}
// clear unordered_map
umap.clear();
// reserve the bucket for at least 10 elements
umap.reserve(10);
// again checking size of unordered_map
cout << "unordered_map is empty: "
<< (umap.empty() ? "True" : "False") << endl;
return 0;
}
Original unordered_map::
Key Value
An Apple
Ball Cat
Cat Dog
Dog Goat
Elephant Fish
unordered_map is empty: True
// C++程序演示无序映射的reserve()
#include <bits/stdc++.h>
using namespace std;
int main()
{
// 声明
unordered_map<char, char> sample1, sample2;
// 将sample1的容量保留为至少1个元素的桶
sample1.reserve(1);
// 向sample1中插入键和元素
sample1.insert({ 'a', 'A' });
sample1.insert({ 'g', 'G' });
// 向sample2中插入键和元素
// 将sample2的容量保留为至少3个元素的桶
sample2.reserve(3);
sample2.insert({ 'b', 'B' });
sample2.insert({ 'c', 'C' });
sample2.insert({ 'd', 'D' });
cout << "Sample1的大小是:" << sample1.size();
cout << "\nSample1的键和元素为:";
for (auto it = sample1.begin(); it != sample1.end(); it++) {
cout << "{" << it->first << ", " << it->second << "} ";
}
cout << "\n\nSample2的大小是:" << sample2.size();
cout << "\nSample2的键和元素为:";
for (auto it = sample2.begin(); it != sample2.end(); it++) {
cout << "{" << it->first << ", " << it->second << "} ";
}
return 0;
}
Sample1的大小是:2
Sample1的键和元素为:{g, G} {a, A}
Sample2的大小是:3
Sample2的键和元素为:{d, D} {c, C} {b, B}