📜  C++ STL中的unordered_multimap max_size()函数

📅  最后修改于: 2021-05-30 09:29:18             🧑  作者: Mango

unordered_multimap :: max_size()是C++ STL中的内置函数,该函数返回unordered_multimap容器可以容纳的最大元素数。

句法:

unordered_multimap_name.max_size()

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

返回值:返回一个整数值,该整数值表示容器可以容纳的元素的最大大小。

下面的程序说明了上述函数:

程序1:

// C++ program to illustrate the
// unordered_multimap::max_size()
#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 number of elements that sample1 can hold: "
         << sample1.size();
  
    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 number of elements that sample1 can hold: 2
Key and Elements of Sample1 are:{50, 500} {10, 100}

程式2:

// C++ program to illustrate the
// unordered_multimap::max_size()
#include 
using namespace std;
  
int main()
{
  
    // declaration
    unordered_multimap sample1, sample2;
  
    // inserts key and element
    // in sample1
    sample1.insert({ 'a', 'A' });
    sample1.insert({ 'g', 'G' });
  
    cout << "The max number of elements that sample1 can hold: "
         << sample1.size();
  
    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 number of elements that sample1 can hold: 2
Key and Elements of Sample1 are:{g, G} {a, A}
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”