map::max_size() function with Example in C++ STL

C++ STL map::max_size() function with example: Here, we are going to learn about the map::max_size() function in C++ STL, that is used to return the maximum number of elements of the map that it can store.
Submitted by Vivek Kothari, on December 03, 2018

C++ STL map::max_size()

It returns the maximum number of elements the container(map) is able to hold but at runtime, the size of the container may be limited to a value smaller than specified by max_size() by the amount of RAM available. It gives us an only a theoretical limit on the size of the container.

Syntax:

    myMap.max_size()

Where, myMap is the object of class map.

Parameters: None - It does not accept any parameters.

Return value: It simply returns the maximum number of elements container can hold.

Example:

#include <bits/stdc++.h> 
using namespace std; 

int main() 
{  
	// create map container 
	map<int, int> myMap;

	//insert an element in map
	myMap.insert( pair<int, int>(200 , 100) ); 

	cout<<"max size of Non-empty map : \n"; 
	cout << "The max size of myMap is " << myMap.max_size();

	map<char,char> EmpMap;
	map<int, int> EmpMap2; 

	cout<<"max size of Empty-map : \n"; 
	cout << "\nThe max size of EmpMap is " << EmpMap.max_size();
	cout << "\nThe max size of EmpMap2 is " << EmpMap2.max_size();

	return 0; 
} 

Output

max size of Non-empty map :
The max size of myMap is 461168601842738790max size of Empty-map :

The max size of EmpMap is 461168601842738790
The max size of EmpMap2 is 461168601842738790



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.