當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


C++ unordered_map reserve()用法及代碼示例


我們知道,存儲桶是容器內部哈希表中的一個插槽,所有元素均基於其鍵的哈希值分配到該插槽。存儲桶的編號從0到bucket_count。現在,作為存儲項的可變數量的存儲桶。這個數字是基於術語Load Factor的,當Load Factor(load_factor)達到某個閾值時,容器會增加存儲桶的數量並重新映射Map,但是當我們調用rehash(n)時,它將直接設置存儲桶的數量到n並觸發整個哈希表的重建。但是當我們調用reserve(n)時,它將創建足夠的Bucket以容納至少n個項目。如果然後我們向Map添加> 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 illustrate the 
// unordered_map::reserve() 
#include <bits/stdc++.h> 
using namespace std; 
  
int main() 
{ 
  
    // declaration 
    unordered_map<char, char> 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({ 'a', 'A' }); 
    sample1.insert({ 'g', 'G' }); 
  
    // 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({ 'b', 'B' }); 
    sample2.insert({ 'c', 'C' }); 
    sample2.insert({ 'd', 'D' }); 
  
    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:{g, G} {a, A} 

The size of Sample2 is:3
Key and Elements of Sample2 are:{d, D} {c, C} {b, B}


相關用法


注:本文由純淨天空篩選整理自YashRaj5大神的英文原創作品 unordered_map reserve() in C++ STL。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。