Here I want to clear the concepts of map in short.There are 4 associate containers (binary tree) : set ,multiset ,map and multimap .Applications of map are very important because, both multiset and maps are sorted binary trees, so inserting/finding 1 out of N entries takes O(log N).As we will focus
include map library :
#include<map>
Declaration:
map<data_type,data_type> map_name ;
ex: map<char ,int> myMap;
char is key type and int is key value.
Insertion :
Various ways :
- myMap.insert(pair <char,int> (‘a’ ,100));
- myMap.insert(make_pair(‘a’,100));
- map<char , int>::iterator it = myMap.begin(); myMap.insert(it ,pair<char,int> (‘a’ ,100));
- myMap[‘a’] =100;
- myMap.insert(map <char,int> ::value_type(‘a’,100));
Finding :
myMap.find(key_type);
size :
myMpa.size(); returns int .
These are the some imp. and simple things you need to know for using map.Now practice some problem using map.
Here is a example I found :
// std::map example // opposite words #include #include
Hope you learnt something , stay tune for more examples and interesting facts about c++.
#keepCoding