unordered_map max_size in C++ STL

The unordered_map::max_size is a built in function in C++ STL. It returns maximum number of elements which unordered_map can hold. Maximum number of elements in any container depends upon system and library implementation.
Syntax

size unordered_map.max_size()

Parameters: It does not accept any parameters.

Return type: Unsigned integer a container can hold maximum elements.



Example 1

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to illustrate the
// unordered_map::max_size function
#include <bits/stdc++.h>
using namespace std;
  
int main()
{
  
    // declaration of unordered_map
    unordered_map<int, int> sample;
  
    cout << " Current size is :  " << sample.size() << endl;
    cout << " max size is : " << sample.max_size() << endl;
  
    // insert elements
    sample.insert({ 1, 10 });
    sample.insert({ 2, 10 });
    sample.insert({ 3, 10 });
    sample.insert({ 4, 10 });
  
    cout << " Current size is :  " << sample.size() << endl;
    cout << " max size is : " << sample.max_size() << endl;
  
    return 0;
}

chevron_right


Output:

Current size is :  0
 max size is : 1152921504606846975
 Current size is :  4
 max size is : 1152921504606846975

Example 2

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to illustrate the
// unordered_map::max_size function
#include <bits/stdc++.h>
using namespace std;
  
int main()
{
  
    // declaration of unordered_map
    unordered_map<char, int> sample;
  
    cout << " Current size is :  " << sample.size() << endl;
    cout << " max size is : " << sample.max_size() << endl;
  
    // insert elements
    sample.insert({ 'a', 10 });
    sample.insert({ 'b', 10 });
    sample.insert({ 'c', 10 });
  
    cout << " Current size is :  " << sample.size() << endl;
    cout << " max size is : " << sample.max_size() << endl;
  
    return 0;
}

chevron_right


Output:

Current size is :  0
 max size is : 1152921504606846975
 Current size is :  3
 max size is : 1152921504606846975

Complexity : Its complexity is constant.



My Personal Notes arrow_drop_up

All powers are within you You can do anything and everything Believe in that

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.