C++ unordered_map::reserve() 函数



C++ 的 std::unordered_map::reserve() 函数用于将容器中的桶数量设置为最适合容纳至少 n 个元素且不超过最大负载因子的数量。

如果 n 大于当前的 bucket_count(),则容器的桶数量会以相同的方式增加;如果 n 小于当前的 bucket_count(),则不会产生任何影响。

语法

以下是 std::unordered_map::reserve() 函数的语法。

void reserve(size_type n);

参数

  • n - 表示容器的新容量。

返回值

此函数为 void 类型,因此不返回任何值。

示例 1

在以下示例中,让我们看看 reserve() 函数的用法。

#include <iostream>
#include <unordered_map>
using namespace std;
int main(void) {
   unordered_map<char, int> um;
   cout << "Initial bucket count = " << um.bucket_count() << endl;
   um.reserve(5);
   cout << "Bucket count after reserve = "<< um.bucket_count() << endl;
   return 0;
}

输出

以上代码的输出如下:

Initial bucket count = 1
Bucket count after reserve = 5

示例 2

考虑以下示例,我们将使桶数量至少可以存储 5 个元素。

#include <iostream>
#include <unordered_map>
using namespace std;
int main () {
   unordered_map<string, string> uMap;
   cout << "Bucket Count: " << uMap.bucket_count() << endl;
   uMap.reserve(5);
   cout << "Bucket Count after reserve(): " << uMap.bucket_count() << endl;
  
   uMap["Fname"] = "tutorials";
   uMap["Lname"] = "Point";
   uMap["Country"] = "India";
   uMap["Locaton"] = "Hyderabad";

   for (auto& it: uMap)
      cout << it.first << "->" << it.second << endl;
   return 0;
}

输出

如果我们运行以上代码,它将生成以下输出:

Bucket Count: 1
Bucket Count after reserve(): 5
Country->India
Lname->Point
Locaton->Hyderabad
Fname->tutorials

示例 3

让我们看一下以下示例,其中我们在使用 reserve() 函数之前和之后显示桶及其元素。

#include <iostream>
#include <unordered_map>
using namespace std;
int main () {
   unordered_map<string, string> uMap={{"Hyderabad", "India"}, {"Delhi", "India"}, {"Bangalore", "India"}};
   cout<<"Unordered_map contains "<<uMap.bucket_count()<<" buckets:";
   for(unsigned int i = 0; i < uMap.bucket_count(); i++) {
      cout<<"\nThe bucket "<<i<<" contains: ";   
      for(auto it = uMap.begin(i); it != uMap.end(i); ++it) {
         cout<<it->first<<":"<<it->second<<" ";
      } 
   }  

   cout<<"\nCapacity is changed using reserve function.\n";
   uMap.reserve(5);
   
   cout<<"Unordered_map contains "<<uMap.bucket_count()<<" buckets:";
   for(unsigned int i = 0; i < uMap.bucket_count(); i++) {
      cout<<"\nThe bucket "<<i<<" contains: ";   
      for(auto it = uMap.begin(i); it != uMap.end(i); ++it) {
         cout<<it->first<<":"<<it->second<<" ";
      } 
   }
   return 0;
}

输出

让我们编译并运行以上程序,这将产生以下结果:

Unordered_map contains 13 buckets:
The bucket 0 contains: 
The bucket 1 contains: Bangalore:India 
The bucket 2 contains: Hyderabad:India 
The bucket 3 contains: Delhi:India 
The bucket 4 contains: 
The bucket 5 contains: 
The bucket 6 contains: 
The bucket 7 contains: 
The bucket 8 contains: 
The bucket 9 contains: 
The bucket 10 contains: 
The bucket 11 contains: 
The bucket 12 contains: 
Capacity is changed using reserve function.
Unordered_map contains 5 buckets:
The bucket 0 contains: Banglore:India 
The bucket 1 contains: 
The bucket 2 contains: 
The bucket 3 contains: Delhi:India 
The bucket 4 contains: Hyderabad:India 
广告