Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




MAP - SIZE() METHOD


The size() Method is used to give us the number of elements in the Map.


Let us say, we have a Map that contains three entries. And we want to check how many elements are there in the Map.


We can use the size() Method to achieve the above.


Example :



#include <iostream>
#include <map>

using namespace std;

int main() {

    map<string, string> x = 
        {
	        { "Five", "Is a Number" }, 
	        { "John", "Is a Name" }, 
	        { "C++", "Is a Language"}
        };
        
    int count = x.size(); 
    cout << "The number of elements in the Map are : " << count;  
    
    return 0;    
}


Output :



  The number of elements in the Map are : 3

So, in the above code we have created a Map and initialised to the variable x.


map<string, string> x =
	{
		{ "Five", "Is a Number" },
		{ "John", "Is a Name" },
		{ "C++", "Is a Language"}
	};

Below is how the values are positioned in the Map,

java_Collections

Next, we have used the size() method to get the number of elements in the Map and store it in a variable count.


int count = x.size();
java_Collections


And when we print the value of count variable, we get 3 as output.