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