unordered_multimap::rehash(N)是C++ STL中的內置函數,該函數將容器中的存儲桶數設置為N或更大。如果N大於容器中的當前存儲桶數(bucket_count),則會強製進行重新哈希處理。
新的存儲區計數可以等於或大於N。如果n小於容器中當前存儲區的數量(bucket_count),該函數可能對存儲區計數沒有影響,也可能不會強製重新哈希。
重新哈希是哈希表的重建:容器中的所有元素根據其哈希值重新排列到新的存儲桶集中。盡管可以保留具有等效鍵的元素的相對順序,但是這可能會更改容器內元素的迭代順序。每當容器重新進行修補時,容器都會自動執行負載係數在操作中將超越其max_load_factor。通過調用rehash在哈希表中保留一定數量的最小存儲桶,我們避免了容器擴展可能引起的多次哈希。
用法:
unordered_multimap_name.rehash(N)
參數:該函數接受單個強製性參數N,該參數指定容器哈希表的最小存儲桶數。
返回值:該函數不返回任何內容。
以下示例程序旨在說明上述函數:
示例1:
// C++ program to illustrate the
// unordered_multimap::rehash()
#include <bits/stdc++.h>
using namespace std;
int main()
{
// declaration
unordered_multimap<int, int> 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 <bits/stdc++.h>
using namespace std;
int main()
{
// declaration
unordered_multimap<char, char> 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++ unordered_multiset rehash()用法及代碼示例
- C++ unordered_set rehash()用法及代碼示例
- C++ unordered_map rehash用法及代碼示例
- C++ fma()用法及代碼示例
- C++ div()用法及代碼示例
- C++ log()用法及代碼示例
- C++ mbsrtowcs()用法及代碼示例
- C++ unordered_map end( )用法及代碼示例
- C++ strcspn()用法及代碼示例
- C++ atexit()用法及代碼示例
注:本文由純淨天空篩選整理自gopaldave大神的英文原創作品 unordered_multimap rehash() function in C++ STL。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。