📌  相关文章
📜  C++ STL中的unordered_multiset load_factor()函数

📅  最后修改于: 2021-05-30 03:42:36             🧑  作者: Mango

unordered_multiset :: load_factor()是C++ STL中的内置函数,该函数返回在unordered_multiset容器中返回当前负载因子。负载因子是容器中元素数量(其大小)与存储桶数量(bucket_count)之间的比率:

负载因子会影响哈希表中发生冲突的概率(即,两个元素位于同一存储桶中的概率)。通过在每次需要扩展时进行一次重新哈希处理,容器会自动增加存储桶的数量,以将负载系数保持在特定阈值(其max_load_factor)以下。

语法

unordered_multiset_name.load_factor

参数:该函数不接受任何参数。

返回值:该函数返回当前的负载系数。它可以是整数或双精度类型。

下面的程序说明了unordered_multiset :: load_factor()函数:

程序1

// C++ program to illustrate the
// unordered_multiset::load_factor() function
#include 
using namespace std;
  
int main()
{
  
    // declaration
    unordered_multiset sample;
  
    // inserts element
    sample.insert('a');
    sample.insert('b');
  
    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');
    sample.insert('b');
  
    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');
  
    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: 2
The bucket_count is: 3
The load_factor is: 0.666667

The size is: 4
The bucket_count is: 7
The load_factor is: 0.571429

The size is: 5
The bucket_count is: 7
The load_factor is: 0.714286

程序2

// C++ program to illustrate the
// unordered_multiset::load_factor() function
#include 
using namespace std;
  
int main()
{
  
    // declaration
    unordered_multiset sample;
  
    // inserts element
    sample.insert(1);
    sample.insert(1);
  
    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);
    sample.insert(2);
  
    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(2);
  
    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: 2
The bucket_count is: 3
The load_factor is: 0.666667

The size is: 4
The bucket_count is: 7
The load_factor is: 0.571429

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