- C++ Library - Home
- C++ Library - <fstream>
- C++ Library - <iomanip>
- C++ Library - <ios>
- C++ Library - <iosfwd>
- C++ Library - <iostream>
- C++ Library - <istream>
- C++ Library - <ostream>
- C++ Library - <sstream>
- C++ Library - <streambuf>
- C++ Library - <atomic>
- C++ Library - <complex>
- C++ Library - <exception>
- C++ Library - <functional>
- C++ Library - <limits>
- C++ Library - <locale>
- C++ Library - <memory>
- C++ Library - <new>
- C++ Library - <numeric>
- C++ Library - <regex>
- C++ Library - <stdexcept>
- C++ Library - <string>
- C++ Library - <thread>
- C++ Library - <tuple>
- C++ Library - <typeinfo>
- C++ Library - <utility>
- C++ Library - <valarray>
- The C++ STL Library
- C++ Library - <array>
- C++ Library - <bitset>
- C++ Library - <deque>
- C++ Library - <forward_list>
- C++ Library - <list>
- C++ Library - <map>
- C++ Library - <multimap>
- C++ Library - <queue>
- C++ Library - <priority_queue>
- C++ Library - <set>
- C++ Library - <multiset >
- C++ Library - <stack>
- C++ Library - <unordered_map>
- C++ Library - <unordered_set>
- C++ Library - <unordered_multiset>
- C++ Library - <vector>
- C++ Library - <algorithm>
- C++ Library - <iterator>
- The C++ Advanced Library
- C++ Library - <any>
- C++ Library - <barrier>
- C++ Library - <bit>
- C++ Library - <chrono>
- C++ Library - <cinttypes>
- C++ Library - <clocale>
- C++ Library - <condition_variable>
- C++ Library - <coroutine>
- C++ Library - <cstdlib>
- C++ Library - <cstring>
- C++ Library - <cuchar>
- C++ Library - <charconv>
- C++ Library - <cfenv>
- C++ Library - <cmath>
- C++ Library - <ccomplex>
- C++ Library - <expected>
- C++ Library - <format>
- C++ Library - <future>
- C++ Library - <flat_set>
- C++ Library - <flat_map>
- C++ Library - <filesystem>
- C++ Library - <generator>
- C++ Library - <initializer_list>
- C++ Library - <latch>
- C++ Library - <memory_resource>
- C++ Library - <mutex>
- C++ Library - <mdspan>
- C++ Library - <optional>
- C++ Library - <print>
- C++ Library - <ratio>
- C++ Library - <scoped_allocator>
- C++ Library - <semaphore>
- C++ Library - <source_location>
- C++ Library - <span>
- C++ Library - <spanstream>
- C++ Library - <stacktrace>
- C++ Library - <stop_token>
- C++ Library - <syncstream>
- C++ Library - <system_error>
- C++ Library - <string_view>
- C++ Library - <stdatomic>
- C++ Library - <variant>
- C++ STL Library Cheat Sheet
- C++ STL - Cheat Sheet
- C++ Programming Resources
- C++ Programming Tutorial
- C++ Useful Resources
- C++ Discussion
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