multimap size() function in C++ STL


In this article we will be discussing the working, syntax, and examples of multimap::size() function in C++ STL.

What is Multimap in C++ STL?

Multimaps are the associative containers, which are similar to map containers. It also facilitates to store the elements formed by a combination of key-value and mapped value in a specific order. In a multimap container there can be multiple elements associated with the same key. The data is internally always sorted with the help of its associated keys.

What is multimap::size()?

multimap::size() function is an inbuilt function in C++ STL, which is defined in <map> header file. size() is used to check the size of the multimap container. This function gives size or we can say gives us the number of elements in the multimap container associated.

Syntax

map_name.size();

Parameters

The function accepts no parameter.

Return value

This function returns the number of elements in the container. If the container has no values the function returns 0.

Input 

std::multimap<int> mymap;
mymap.insert(make_pair(‘a’, 10));
mymap.insert(make_pair(‘b’, 20));
mymap.insert(make_pair(‘c’, 30));
mymap.size();

Output 

3

Input 

std::multimap<int> mymap;
mymap.size();

Output 

0

Example

 Live Demo

#include<iostream>
#include<map>
using namespace std;
int main(){
   multimap<int,int > mul_1;
   multimap<int,int> mul_2;
   //declaring iterator to traverse the elements
   multimap<int,int&g;:: iterator i;
   //inserting elements to multimap1
   mul_1.insert({1,10});
   mul_1.insert({2,20});
   mul_1.insert({3,30});
   mul_1.insert({4,40});
   mul_1.insert({5,50});
   //checking the number of elements in multimap1
   cout<"Total number of elements in multimap1 are: "<<mul_1.size();
   cout<<"\nElements in multimap1 are: "<<"\n";
   for( i = mul_1.begin(); i!= mul_1.end(); i++){
      cout<<(*i).first<<" "<< (*i).second << "\n";
   }
}

Output

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

Total number of elements in multimap1 are: 5
Elements in multimap1 are:
1 10
2 20
3 30
4 40
5 50

Updated on: 22-Apr-2020

252 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements