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


C++ unordered_multiset rehash()用法及代碼示例


unordered_multiset::rehash()是C++ STL中的內置函數,它將容器中的存儲桶數設置為N或更大。如果N大於容器中的當前存儲桶數(bucket_count),則將強製進行重新哈希處理。

新的存儲桶計數可以等於或大於N。如果n小於容器中的當前存儲桶數(bucket_count),則該函數可能對存儲桶計數沒有影響,並且可能不會強製進行重新哈希。

重新哈希是哈希表的重建:容器中的所有元素根據其哈希值重新排列到新的存儲桶集中。盡管可以保留具有等效鍵的元素的相對順序,但是這可能會更改容器內元素的迭代順序。隻要容器的裝載係數在操作中超過其max_load_factor,容器就會自動執行修複。通過調用rehash在哈希表中保留一定數量的最小存儲桶,我們避免了容器擴展可能引起的多次哈希。


用法:

unordered_multset_name.rehash(N);

參數:該函數僅接受如下所列的一個參數:

  • count:這是新的存儲桶數量。

返回值:無(無效類型函數)

以下示例程序旨在說明上述方法:

示例1:

// C++ program to illustrate 
// unordered_multiset::rehash() 
#include <iostream> 
#include <unordered_set> 
using namespace std; 
  
// function to diplay values in multiset 
void display(unordered_multiset<int> s) 
{ 
    for (auto it = s.begin(); it != s.end(); it++) 
        cout << *it << endl; 
} 
  
int main() 
{ 
    // declaration 
    unordered_multiset<int> s1; 
  
    // inserting initial values 
    s1.insert(1); 
    s1.insert(2); 
    s1.insert(3); 
  
    // displaying Initial values 
    cout << "Initial values are:\n"; 
    display(s1); 
    cout << endl; 
  
    // diplaying initial parameters 
    cout << "initial parameters are: \n"; 
    cout << "bucketcount() = " << s1.bucket_count() << endl; 
    cout << "load factor = " << s1.load_factor() << endl; 
    cout << "Max_load_factor = " << s1.max_load_factor() << endl; 
    cout << endl; 
  
    // performing rehash 
    s1.rehash(200); 
  
    // displaying final parameters 
    cout << "final parameters are: \n"; 
    cout << "bucketcount() = " << s1.bucket_count() << endl; 
    cout << "load factor = " << s1.load_factor() << endl; 
    cout << "Max_load_factor = " << s1.max_load_factor() << endl; 
    cout << endl; 
  
    return 0; 
}
輸出:
Initial values are:
3
1
2

initial parameters are: 
bucketcount() = 7
load factor = 0.428571
Max_load_factor = 1

final parameters are: 
bucketcount() = 211
load factor = 0.014218
Max_load_factor = 1

示例2:

// C++ program to illustrate 
// unordered_multiset::rehash() 
#include <iostream> 
#include <unordered_set> 
using namespace std; 
  
// function to diplay values in multiset 
void display(unordered_multiset<char> s) 
{ 
    for (auto it = s.begin(); it != s.end(); it++) 
        cout << *it << endl; 
} 
int main() 
{ 
    // declaration 
    unordered_multiset<char> s1; 
  
    // inserting initial values 
    s1.insert('a'); 
    s1.insert('b'); 
    s1.insert('c'); 
  
    // displaying Initial values 
    cout << "Initial values are:\n"; 
    display(s1); 
    cout << endl; 
  
    // diplaying initial parameters 
    cout << "initial parameters are: \n"; 
    cout << "bucketcount() = " << s1.bucket_count() << endl; 
    cout << "load factor = " << s1.load_factor() << endl; 
    cout << "Max_load_factor = " << s1.max_load_factor() << endl; 
    cout << endl; 
  
    // performing rehash 
    s1.rehash(200); 
  
    // displaying final parameters 
    cout << "final parameters are: \n"; 
    cout << "bucketcount() = " << s1.bucket_count() << endl; 
    cout << "load factor = " << s1.load_factor() << endl; 
    cout << "Max_load_factor = " << s1.max_load_factor() << endl; 
    cout << endl; 
      
    return 0; 
}
輸出:
Initial values are:
c
a
b

initial parameters are: 
bucketcount() = 7
load factor = 0.428571
Max_load_factor = 1

final parameters are: 
bucketcount() = 211
load factor = 0.014218
Max_load_factor = 1


相關用法


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