map::size() in C++ STL

C++Server Side ProgrammingProgramming

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

What is a Map in C++ STL?

Maps are the associative container, which facilitates to store the elements formed by a combination of key value and mapped value in a specific order. In a map container the data is internally always sorted with the help of its associated keys. The values in the map container are accessed by its unique keys.

What is a map::size()?

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

Syntax

map_name.size();

Parameters

The function accepts no parameter.

Return value

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

Example

Input

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

Output

3

Input

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

Output

0

Example

 Live Demo

#include <bits/stdc++.h>
using namespace std;
int main() {
   map<int, int> TP_1;
   TP_1[1] = 10;
   TP_1[2] = 20;
   TP_1[3] = 30;
   TP_1[4] = 40;
   cout<<"Size of TP_1 is: "<<TP_1.size();
   return 0;
}

Output

Size of TP_1 is: 4

Example

 Live Demo

#include <bits/stdc++.h>
using namespace std;
int main() {
   map<int, int> TP_1;
   TP_1[1] = 10;
   TP_1[2] = 20;
   TP_1[3] = 30;
   TP_1[4] = 40;
   auto size = TP_1.size();
   auto temp = 1;
   while(size!=0) {
      temp = temp * 10;
      size--;
   }
   cout<<"Temp value is: "<<temp<<endl;
   return 0;
}

Output

Temp value is: 10000
raja
Published on 15-Apr-2020 12:55:15
Advertisements