C++ unordered_multimap::max_size() Function



The C++ std::unordered_multimap::max_size() function is used to return the maximum number of elements that can be held by unordered_multimap or container. This number depends on the system or library implementation.

If we try to use the max_size() function in the same program with different unordered_multimaps or empty unordered_multimaps, we get the same maximum size of the unordered_multimap container.

Syntax

Following is the syntax of std::unordered_multimap::max_size() function.

size_type max_size() const;

Parameters

This function does not accepts any parameter.

Return value

This function returns the maximum number of elements that can be held by unordered_multimap.

Example 1

In the following example, we are demonstrating the usage of the unordered_multimap::max_size()function.

#include <iostream>
#include <unordered_map>
using namespace std;
int main(void) {
   unordered_multimap<char, int> umm;
   cout << "max_size of unordered_multimap = " << umm.max_size() << endl;
   return 0;
}

Output

Let us compile and run the above program, this will produce the following result −

max_size of unordered_multimap = 576460752303423487

Example 2

Consider the following example, where we are going to use the max_size() function and getting the maximum size of the container before and after the insertion of elements into the empty multimap.

#include <iostream>
#include <unordered_map>
using namespace std;
int main(void) {
   unordered_multimap<char, int> um;
   cout << "max_size of unordered_multimap = " << um.max_size() << endl;
   um.insert({{'A', 2}, {'B', 5}, {'C', 6}, {'D', 8}, {'D', 10}});
   cout<<"*** Maximum size of unordered multimap after inserting the element to it ***"<<endl;
   cout << "max_size of unordered_multimap = " << um.max_size() << endl;
   return 0;
}

Output

If we run the above code it will generate the following output −

max_size of unordered_multimap = 576460752303423487
*** Maximum size of unordered multimap after inserting the element to it ***
max_size of unordered_multimap = 576460752303423487

Example 3

Let's look at the following example, where we are going to check whether the maximum size of the two multimaps with different elements are same or not.

#include <iostream>
#include <unordered_map>
using namespace std;
int main(void) {
   unordered_multimap<int, int> ummap, umm;
   cout << "max_size of unordered_multimap = " << ummap.max_size() << endl;
   cout << "max_size of unordered_multimap = " << umm.max_size() << endl;
   ummap.insert({{1, 2}, {2, 5}, {3, 6}, {4, 10}});
   umm.insert({{2, 2}, {3, 5}, {5, 6}, {6, 10}});
   cout<<"*** Maximum size of unordered multimap after inserting the element to it ***"<<endl;
   cout << "max_size of unordered_multimap = " << ummap.max_size() << endl;
   cout << "max_size of unordered_multimap = " << umm.max_size() << endl;
   return 0;
}

Output

Following is the output of the above code −

max_size of unordered_multimap = 576460752303423487
max_size of unordered_multimap = 576460752303423487
*** Maximum size of unordered multimap after inserting the element to it ***
max_size of unordered_multimap = 576460752303423487
max_size of unordered_multimap = 576460752303423487

Example 4

Following is the example, where we are going to use the size() and max_size() functions to compare between normal size and max_size.

#include <iostream>
#include <unordered_map>
using namespace std;
int main(void) {
   unordered_multimap<int, int> ummap = {{1, 10}, {1, 100}, {2, 20}, {2, 200}, {3, 30}};
   cout<<"size of the ummap: "<<ummap.size()<<endl;
   cout<<"Maximum size of the ummap: "<<ummap.max_size();
   return 0;
}

Output

Output of the above code is as follows −

size of the ummap: 5
Maximum size of the ummap: 576460752303423487
Advertisements