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 
廣告