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


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

unordered_multimap::max_load_factor()是C++ STL中的内置函数,该函数返回unordered_multimap容器的最大加载因子。此函数还提供设置最大负载系数的选项。

  1. 用法(返回最大负载系数):
    unordered_multimap_name.max_load_factor()

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

    返回值:它返回一个整数值,该值表示容器的最大负载系数。


    以下示例程序旨在说明上述函数:

    示例1:

    // C++ program to illustrate the 
    // unordered_multimap::max_size() 
    #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}
    
  2. 用法(设置最大负载系数):
    unordered_multimap_name.max_load_factor(N)

    参数:该函数接受单个强制参数N,该参数指定要设置的负载系数。该N将是容器的最大负载系数。

    返回值:该函数不返回任何内容。

    以下示例程序旨在说明上述函数:

    // C++ program to illustrate the 
    // unordered_multimap::max_size() 
    #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}
    


相关用法


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