C++ STL中的unordered_map::max_load_factor是内置函数,用于获取和设置unordered_map中的最大负载系数。负载系数是容器中元件数与铲斗数之间的比率。默认情况下,unordered_map max_load系数为1.0。语法:max_load_factor有两种函数。
- float max_load_factor()
- void max_load_factor(float new_size)
返回类型:仅第一个版本返回max_load_factor。
参数:只有第二个版本接受新的尺寸。
Note:
- 第一版返回最大负载系数。
- 第二个版本设置了新的负载系数。
例子1
// C++ program to illustrate the
// unordered_map::max_bucket_count function
#include <bits/stdc++.h>
using namespace std;
int main()
{
// declaration of unordered_map
unordered_map<char, int> sample;
// insert elements
sample.insert({ 'a', 10 });
sample.insert({ 'b', 10 });
sample.insert({ 'c', 10 });
sample.insert({ 'd', 10 });
sample.insert({ 'e', 10 });
sample.insert({ 'f', 10 });
cout << "Current size is: " << sample.size() << endl;
cout << "Current load factor is:" << sample.load_factor() << endl;
cout << "Current Max load factor is " << sample.max_load_factor() << endl;
// Changing max load factor
sample.max_load_factor(5.0 / 2.0);
cout << "Current size is: " << sample.size() << endl;
cout << "Current load factor is:" << sample.load_factor() << endl;
cout << "Current Max load factor is " << sample.max_load_factor() << endl;
return 0;
}
输出:
Current size is: 6 Current load factor is:0.857143 Current Max load factor is 1 Current size is: 6 Current load factor is:0.857143 Current Max load factor is 2.5
例子2
// C++ program to illustrate the
// unordered_map::max_bucket_count function
#include <bits/stdc++.h>
using namespace std;
int main()
{
// declaration of unordered_map
unordered_map<int, int> sample;
// insert elements
sample.insert({ 1, 10 });
sample.insert({ 2, 10 });
sample.insert({ 3, 10 });
sample.insert({ 4, 10 });
cout << " Current size is: " << sample.size() << endl;
cout << " Current load factor is:" << sample.load_factor() << endl;
cout << " Current Max load factor is " << sample.max_load_factor() << endl;
// Changing max load factor
sample.max_load_factor(5.0 / 2.0);
cout << " Current size is: " << sample.size() << endl;
cout << " Current load factor is:" << sample.load_factor() << endl;
cout << " Current Max load factor is " << sample.max_load_factor() << endl;
return 0;
}
输出:
Current size is: 4 Current load factor is:0.571429 Current Max load factor is 1 Current size is: 4 Current load factor is:0.571429 Current Max load factor is 2.5
复杂:O(1)。
相关用法
注:本文由纯净天空筛选整理自ankit15697大神的英文原创作品 unordered_map max_load_factor in C++ STL。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。