C++ unordered_set::reserve() 函数



C++ 的unordered_set::reserve()函数用于更改桶的容量。它将容器中的桶数 (bucket_count) 设置为最适合容纳至少 n 个元素且不超过最大负载因子的值,并重新哈希容器。

当 n 大于当前 bucket_count 乘以 max_load_factor 时,会强制进行重新哈希,并且容器桶计数的增加会导致重新哈希。

语法

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

void reserve ( size_type n );

参数

  • n − 表示桶的最小数量。

返回值

此函数不返回任何值。

示例 1

让我们来看下面的例子,我们将演示 unordered_set::reserve() 函数的用法。

#include <iostream>
#include <string>
#include <unordered_set>
using namespace std;

int main () {
   unordered_set<string> uSet;
   
   uSet.reserve(3);
   uSet.insert("android");
   uSet.insert("java");
   uSet.insert("html");
   
   cout << "uSet contains:";
   
   for (const string& x: uSet) cout << " " << x;
   cout << endl;
   
   cout<<"After reserve bucket count is: "<< uSet.bucket_count();
   return 0;
}

输出

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

uSet contains: html java android
After reserve bucket count is: 3

示例 2

在下面的例子中,我们将计算使用 unordered_set::reserve() 函数前后桶的数量。

#include <iostream>
#include <string>
#include <unordered_set>
using namespace std;

int main () {
   unordered_set<string> uSet = {"android", "java", "Html", "CSS"};
   cout<<"Initial bucket count is: "<< uSet.bucket_count() << endl;
   
   uSet.reserve(3);
   
   cout << "uSet contains:";
   for (const string& x: uSet)
      cout << " " << x;
   cout << endl;
   cout<<"After reserve bucket count is: "<< uSet.bucket_count();
   return 0;
}

输出

以下是上述代码的输出:

Initial bucket count is: 13
uSet contains: android java Html CSS
After reserve bucket count is: 5

示例 3

考虑下面的例子,我们将显示使用 unordered_set::reserve() 函数前后桶及其元素。

#include <iostream>
#include <unordered_set>
using namespace std;

int main () {
   unordered_set<string> uSet={"Hyderabad", "Delhi", "Bangalore"};
   cout<<"unordered_set contains "<<uSet.bucket_count()<<" buckets:";
   for(unsigned int i = 0; i < uSet.bucket_count(); i++) {
      cout<<"\nThe bucket "<<i<<" contains: ";   
      for(auto it = uSet.begin(i); it != uSet.end(i); ++it) {
         cout<<*it<<" ";
	  } 
   }  

   cout<<"\n***Capacity is changed using reserve function***\n";
   uSet.reserve(5);
   
   cout<<"unordered_set contains "<<uSet.bucket_count()<<" buckets:";
   for(unsigned int i = 0; i < uSet.bucket_count(); i++) {
      cout<<"\nThe bucket "<<i<<" contains: ";   
      for(auto it = uSet.begin(i); it != uSet.end(i); ++it) {
         cout<<*it<<" ";
      } 
   }
   return 0;
}

输出

上述代码的输出如下:

unordered_set contains 13 buckets:
The bucket 0 contains: Bangalore 
The bucket 1 contains: 
The bucket 2 contains: Hyderabad 
The bucket 3 contains: Delhi 
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_set contains 5 buckets:
The bucket 0 contains: 
The bucket 1 contains: Bangalore 
The bucket 2 contains: 
The bucket 3 contains: Delhi 
The bucket 4 contains: Hyderabad 
广告