C++ unordered_multimap max_size () 函数用法


发布日期 : 2019-03-08 09:10:24 UTC

访问量: 10 次浏览

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

unordered\_multimap::max\_size() 是 C++ STL 中的一种内置函数,用于返回 unordered\_multimap 容器可以容纳的最大元素数量。

语法:

unordered_multimap_name.max_size()

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

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

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

程序 1:

// C++ program to illustrate the
// unordered_multimap::max_size()
#include <bits/stdc++.h>
using namespace std;

int main()
{

    // 声明
    unordered_multimap<int, int> sample1;

    // 在 sample1 中插入键和元素
    sample1.insert({ 10, 100 });
    sample1.insert({ 50, 500 });

    cout << "sample1 能够容纳的最大元素数量: "
          << sample1.size();

    cout << "\nSample1 的键和元素是:";
    for (auto it = sample1.begin(); it != sample1.end(); it++) {
        cout << "{" << it->first << ", " << it->second << "} ";
    }

    return 0;
}
sample1 能够容纳的最大元素数量: 2
Sample1 的键和元素是:{50, 500} {10, 100}

程序 2:

// C++ program to illustrate the
// unordered_multimap::max_size()
#include <bits/stdc++.h>
using namespace std;

int main()
{

    // 声明
    unordered_multimap<char, char> sample1, sample2;

    // 在 sample1 中插入键和元素 
    sample1.insert({ 'a', 'A' });
    sample1.insert({ 'g', 'G' });

    cout << "sample1 能够容纳的最大元素数量: "
          << sample1.size();

    cout << "\nSample1 的键和元素是:";
    for (auto it = sample1.begin(); it != sample1.end(); it++) {
        cout << "{" << it->first << ", " << it->second << "} ";
    }

    return 0;
}
sample1 能够容纳的最大元素数量: 2
Sample1 的键和元素是:{g, G} {a, A}