map
es un contenedor que almacena elementos en pares clave-valor. Es similar a las colecciones en Java, matrices asociativas en PHP u objetos en JavaScript.
Estos son los principales beneficios de usar map
:
map
solo almacena claves únicas, y las claves mismas están ordenadas- Debido a que las claves ya están en orden, la búsqueda de un elemento es muy rápida.
- Solo hay un valor para cada clave
Aquí hay un ejemplo:
#include #include using namespace std; int main (){ map first; //initializing first['a']=10; first['b']=20; first['c']=30; first['d']=40; map::iterator it; for(it=first.begin(); it!=first.end(); ++it){ cout
Output:
a => 10 b => 20 c => 30 d => 40
Creating a map
object
map
objectmap myMap;
Insertion
Inserting data with insert member function.
myMap.insert(make_pair("earth", 1)); myMap.insert(make_pair("moon", 2));
We can also insert data in std::map using operator [] i.e.
myMap["sun"] = 3;
Accessing map
elements
map
elementsTo access map elements, you have to create iterator for it. Here is an example as stated before.
map::iterator it; for(it=first.begin(); it!=first.end(); ++it){ cout

Original text
Contribute a better translation