función C ++ STL mapa :: size () con el Ejemplo : Aquí, vamos a aprender sobre la mapa :: función size () en C STL ++ con un ejemplo .
Los mapas son una parte de la C ++ STL y valores de clave en los mapas se utilizan generalmente para identificar los elementos es decir, hay un valor asociado con cada tecla. mapa :: size () está incorporado en la función en C ++ utilizado para encontrar el tamaño del mapa . La complejidad de tiempo de esta función es constante es decir O (1) .
Sintaxis:
MapName.size()
class Valor:
Devuelve el tamaño del mapa es decir, número de elementos del mapa contenedor tener.
Parámetros:
No existe ningún parámetro para ser aprobado.
Ejemplo:
MyMap ={
{1, "c++"},
{2, "java"},
{3, "Python"},
{4, "HTML"},
{5,"PHP"}
};
Aquí, MyMap.size () rendimientos 5 .
Programa:
#include <bits/stdc++.h>
using namespace std;
int main()
{
map<int, string> MyMap1, MyMap2;
MyMap1.insert(pair <int, string> (1, "c++"));
MyMap1.insert(pair <int, string> (2, "java"));
MyMap1.insert(pair <int, string> (3, "Python"));
MyMap1.insert(pair <int, string> (4, "HTML"));
MyMap1.insert(pair <int, string> (5, "PHP"));
map <int, string> :: iterator it;
cout<<"Elements in MyMap1 : "<<endl;
for (it = MyMap1.begin(); it != MyMap1.end(); it++)
{
cout <<"key = "<< it->first <<" value = "<< it->second <<endl;
}
cout<<endl;
cout << "size of MyMap1 : " << MyMap1.size()<<endl;
cout << "size of MyMap2 : " << MyMap2.size();
return 0;
}
salida
Elements in MyMap1 :
key = 1 value = c++
key = 2 value = java
key = 3 value = Python
key = 4 value = HTML
key = 5 value = PHP
size of MyMap1 : 5
size of MyMap2 : 0