當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。