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


C++ unordered_multimap load_factor()用法及代码示例


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


相关用法


注:本文由纯净天空筛选整理自gopaldave大神的英文原创作品 unordered_multimap load_factor() function in C++ STL。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。