C++ STL 中 unordered_multimap max_load_factor() 函数
在 C++ STL 中, unordered_multimap::max_load_factor() 是一个内置函数,返回 unordered_multimap 容器的最大负载系数。该函数还提供了设置最大负载系数的选项。
- 语法: (返回最大负载系数):
unordered_multimap_name_.max_load_factor()
参数: 该函数不接受任何参数。
返回值: 返回一个整数值,表示容器的最大负载系数。
以下程序说明了上述函数:
程序1:
// C++ program to illustrate the
// unordered_multimap::max_load_factor()
#include <bits/stdc++.h>
using namespace std;
int main()
{
// declaration
unordered_multimap<int, int> sample1;
// inserts key and element
// in sample1
sample1.insert({ 10, 100 });
sample1.insert({ 50, 500 });
// prints the max load factor
cout << "The max load factor of sample1: "
<< sample1.max_load_factor();
cout << "\nKey and Elements of Sample1 are:";
for (auto it = sample1.begin(); it != sample1.end(); it++) {
cout << "{" << it->first << ", " << it->second << "} ";
}
return 0;
}
输出:
The max load factor of sample1: 1
Key and Elements of Sample1 are:{50, 500} {10, 100}
- 语法: (设置最大负载系数):
unordered_multimap_name_.max_load_factor(N)
参数: 该函数接受单个强制参数 N
,该参数指定要设置的负载系数。这个 N
将是容器的最大负载系数。
返回值: 该函数不返回任何内容。
以下程序说明了上述函数:
// C++ program to illustrate the
// unordered_multimap::max_load_factor(N)
#include <bits/stdc++.h>
using namespace std;
int main()
{
// declaration
unordered_multimap<int, int> sample1;
// inserts key and element
// in sample1
sample1.insert({ 10, 100 });
sample1.insert({ 50, 500 });
cout << "The max load factor of elements of sample1: "
<< sample1.max_load_factor();
// sets the load factor
sample1.max_load_factor(100);
cout << "\nThe max load factor of sample1 after setting it: "
<< sample1.max_load_factor();
cout << "\nKey and Elements of Sample1 are:";
for (auto it = sample1.begin(); it != sample1.end(); it++) {
cout << "{" << it->first << ", " << it->second << "} ";
}
return 0;
}
输出:
The max load factor of elements of sample1: 1
The max load factor of sample1 after setting it: 100
Key and Elements of Sample1 are:{50, 500} {10, 100}