📜  C++ STL中的unordered_multimap rehash()函数

📅  最后修改于: 2021-05-30 18:56:27             🧑  作者: Mango

unordered_multimap :: rehash(N)是C++ STL中的内置函数,可将容器中的存储桶数设置为N或更大。如果N大于容器中当前的存储桶数(bucket_count),则将强制进行重新哈希处理。
新的存储桶计数可以等于或大于N。如果n小于容器中当前存储桶的数量(bucket_count),则该函数可能对存储桶计数没有影响,并且可能不会强制进行重新哈希。
重新哈希是哈希表的重建:容器中的所有元素根据其哈希值重新排列到新的存储桶集中。尽管可以保留具有等效键的元素的相对顺序,但是这可能会更改容器内元素的迭代顺序。只要容器的负载因子在操作中超过其max_load_factor,容器就会自动执行重整。通过调用rehash在哈希表中保留一定数量的最小存储桶,我们避免了容器扩展可能导致的多次rehash。

句法:

unordered_multimap_name.rehash(N)

参数:该函数接受单个强制性参数N ,该参数N指定容器哈希表的最小存储桶数。

返回值:该函数不返回任何内容。

下面的程序说明了上述函数:

程序1:

// C++ program to illustrate the
// unordered_multimap::rehash()
#include 
using namespace std;
  
int main()
{
  
    // declaration
    unordered_multimap sample1, sample2;
  
    // the sample1 size is reserved for
    // the bucket to contain a minimum of
    // one elements
    sample1.rehash(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.rehash(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: 3
Key and Elements of Sample2 are:{30, 150} {30, 300} {20, 200}

程式2:

// C++ program to illustrate the
// unordered_multimap::rehash()
#include 
using namespace std;
  
int main()
{
  
    // declaration
    unordered_multimap sample1, sample2;
  
    // the sample1 size is reserved for
    // the bucket to contain a minimum of
    // one elements
    sample1.rehash(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.rehash(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}

参考: http : //www.cplusplus.com/reference/unordered_map/unordered_multimap/rehash/

要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”