C++ unordered_multiset Library - max_size() Function



Description

The unordered_multiset::max_size() function in C++ STL returns the maximum number of elements that an unordered_multiset container can hold. It does not accept any parameters.

Syntax

Following is the syntax of unordered_multiset::max_size() function −

size_type max_size() const noexcept;

Return value

It returns a value of type size_type. It represents the maximum number of elements that the unordered_multiset container can hold.

Exceptions

It never throws exceptions.

Time complexity

The time complexity of unordered_multiset::max_size() function is constant.

Examples of unordered_multiset::max_size() Function

The following examples demonstrate the usage of unordered_multiset::max_size() function in unordered_multiset −

Getting the Maximum Size of unordered_multiset

In this example, we are getting the maximum number of elements that the unordered_multiset numbers can hold using the unordered_multiset::max_size() function.

#include <iostream>
#include <unordered_set>
using namespace std;
void printUnorderedMultiset(const unordered_multiset<int>& numbers) {
    for (auto it = numbers.begin(); it != numbers.end(); ++it)
        cout << " " << *it;
    cout << endl;
}
int main() {
    unordered_multiset<int> numbers = {10, 20, 30, 20, 5, 10, 20};
    cout << "Unordered_multiset elements:";
    printUnorderedMultiset(numbers);
    cout << "Current size: " << numbers.size() << endl;
    cout << "Maximum size: " << numbers.max_size() << endl;
    cout << "Available capacity: " 
         << (numbers.max_size() - numbers.size()) << endl;
    return 0;
}

The output of the above code is given below −

Unordered_multiset elements: 20 20 20 10 10 5 30
Current size: 7
Maximum size: 461168601842738790
Available capacity: 461168601842738783

Comparing unordered_multiset::max_size() With Different Data Types

In this example, we compare the maximum size of unordered_multiset containers with different data types. Each data type will have different maximum size −

#include <iostream>
#include <unordered_set>
using namespace std;
int main() {
    unordered_multiset<int> intUnorderedMultiset;
    unordered_multiset<char> charUnorderedMultiset;
    unordered_multiset<double> doubleUnorderedMultiset;
    cout << "Maximum size of int unordered_multiset: " 
         << intUnorderedMultiset.max_size() << endl;
    cout << "Maximum size of char unordered_multiset: " 
         << charUnorderedMultiset.max_size() << endl;
    cout << "Maximum size of double unordered_multiset: " 
         << doubleUnorderedMultiset.max_size() << endl;
    return 0;
}

The output of the above code is given below −

Maximum size of int unordered_multiset: 461168601842738790
Maximum size of char unordered_multiset: 384307168202282325
Maximum size of double unordered_multiset: 461168601842738790

Checking Available Space Before Insertion

The following example checks if there is available space in the unordered_multiset characters before inserting any new value. We get the maximum size using the max_size() function, compare it with the current size, and insert new values if there is available space in the container.

#include <iostream>
#include <unordered_set>
using namespace std;
int main() {
    unordered_multiset<string> characters = {"Luffy", "Zoro", "Shanks"};
    cout << "Characters in unordered_multiset:";
    for (const auto &ch : characters)
        cout << " " << ch;
    cout << "\nCurrent size: " << characters.size() << endl;
    cout << "Maximum size of the container: " << characters.max_size() << endl;
    if (characters.size() < characters.max_size()) {
        characters.insert("Nami");
        characters.insert("Sanji");
        cout << "New size: " << characters.size() << endl;
    }
    else
        cout << "\nUnordered_multiset has reached maximum capacity." << endl;
    cout << "Characters after insertions:";
    for (const auto &ch : characters)
        cout << " " << ch;
    return 0;
}

The output of the above code is given below −

Characters in unordered_multiset: Shanks Zoro Luffy
Current size: 3
Maximum size of the container: 144115188075855871
New size: 5
Characters after insertions: Sanji Nami Shanks Zoro Luffy
multiset.htm
Advertisements