unordered_multimap::load_factor()是C++ STL中的內置函數,該函數返回unordered_multimap容器中的當前負載係數。負載係數是容器中的元素數量(其大小)與鏟鬥數量(bucket_count)之比:
load_factor = size / bucket_count
負載因子會影響哈希表中發生衝突的概率(即,兩個元素位於同一存儲桶中的概率)。通過在每次需要擴展時進行重新哈希處理,容器會自動增加鏟鬥數,以將負載係數保持在特定閾值以下(其max_load_factor)。
用法:
unordered_multimap_name.load_factor()
參數:該函數不接受任何參數。
返回值:該函數返回當前的負載係數。它可以是整數或雙精度類型。
以下示例程序旨在說明unordered_multimap::load_factor()函數:
程序1:
// C++ program to illustrate the
// unordered_multimap::load_factor() function
#include <iostream>
#include <unordered_map>
using namespace std;
int main()
{
// declaration
unordered_multimap<int, int> sample;
// inserts element
sample.insert({ 1, 2 });
sample.insert({ 1, 3 });
sample.insert({ 2, 4 });
sample.insert({ 5, 8 });
sample.insert({ 7, 10 });
cout << "The size is: " << sample.size();
cout << "\nThe bucket_count is: "
<< sample.bucket_count();
cout << "\nThe load_factor is: "
<< sample.load_factor();
sample.insert({ 1, 1 });
sample.insert({ 9, 0 });
cout << "\n\nThe size is: "
<< sample.size();
cout << "\nThe bucket_count is: "
<< sample.bucket_count();
cout << "\nThe load_factor is: "
<< sample.load_factor();
sample.insert({ 1, 1 });
cout << "\n\nThe size is: "
<< sample.size();
cout << "\nThe bucket_count is: "
<< sample.bucket_count();
cout << "\nThe load_factor is: "
<< sample.load_factor();
return 0;
}
輸出:
The size is: 5 The bucket_count is: 7 The load_factor is: 0.714286 The size is: 7 The bucket_count is: 17 The load_factor is: 0.411765 The size is: 8 The bucket_count is: 17 The load_factor is: 0.470588
程序2:
// C++ program to illustrate the
// unordered_multimap::load_factor()
#include <iostream>
#include <unordered_map>
using namespace std;
int main()
{
// declaration
unordered_multimap<char, char> sample;
// inserts element
sample.insert({ 'a', 'b' });
sample.insert({ 'a', 'b' });
sample.insert({ 'a', 'd' });
sample.insert({ 'b', 'e' });
sample.insert({ 'b', 'd' });
cout << "The size is: " << sample.size();
cout << "\nThe bucket_count is: "
<< sample.bucket_count();
cout << "\nThe load_factor is: "
<< sample.load_factor();
sample.insert({ 'b', 'k' });
sample.insert({ 'b', 'h' });
cout << "\n\nThe size is: " << sample.size();
cout << "\nThe bucket_count is: "
<< sample.bucket_count();
cout << "\nThe load_factor is: "
<< sample.load_factor();
sample.insert({ 'z', 'j' });
cout << "\n\nThe size is: " << sample.size();
cout << "\nThe bucket_count is: "
<< sample.bucket_count();
cout << "\nThe load_factor is: "
<< sample.load_factor();
return 0;
}
輸出:
The size is: 5 The bucket_count is: 7 The load_factor is: 0.714286 The size is: 7 The bucket_count is: 17 The load_factor is: 0.411765 The size is: 8 The bucket_count is: 17 The load_factor is: 0.470588
相關用法
- C++ log()用法及代碼示例
- C++ div()用法及代碼示例
- C++ fma()用法及代碼示例
- C++ map key_comp()用法及代碼示例
- C++ wcsncpy()用法及代碼示例
- C++ imag()用法及代碼示例
- C++ real()用法及代碼示例
- C++ valarray end()用法及代碼示例
- C++ regex_iterator()用法及代碼示例
- C++ valarray cos()用法及代碼示例
注:本文由純淨天空篩選整理自gopaldave大神的英文原創作品 unordered_multimap load_factor() function in C++ STL。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。