当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。