unordered_multimap :: max_load_factor()是C++ STL中的内置函数,它返回unordered_multimap容器的最大加载因子。此函数还提供设置最大负载系数的选项。
- 语法(返回最大负载系数):
unordered_multimap_name.max_load_factor()
参数:该函数不接受任何参数。
返回值:它返回一个整数值,该整数值表示容器的最大装载系数。
下面的程序说明了上述函数:
程序1:
C++
// C++ program to illustrate the
// unordered_multimap::max_load_factor()
#include
using namespace std;
int main()
{
// declaration
unordered_multimap 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;
}
C++
// C++ program to illustrate the
// unordered_multimap::max_load_factor(N)
#include
using namespace std;
int main()
{
// declaration
unordered_multimap 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 sample1: 1
Key and Elements of Sample1 are:{50, 500} {10, 100}
- 语法(设置最大负载系数):
unordered_multimap_name.max_load_factor(N)
参数:该函数接受单个强制参数N,该参数指定要设置的负载系数。该N将是容器的最大负载系数。
返回值:该函数不返回任何内容。
下面的程序说明了上述函数:
C++
// C++ program to illustrate the
// unordered_multimap::max_load_factor(N)
#include
using namespace std;
int main()
{
// declaration
unordered_multimap 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}
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程” 。