map
е контейнер, който съхранява елементи в двойки ключ-стойност. Подобно е на колекции в Java, асоциативни масиви в PHP или обекти в JavaScript.
Ето основните предимства на използването map
:
map
съхранява само уникални ключове, а самите ключове са в сортиран ред- Тъй като ключовете вече са в ред, търсенето на елемент е много бързо
- За всеки ключ има само една стойност
Ето пример:
#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