Map

#include <map>  

map<string, int> mapp;                                     // create a map of string: {integer}

// create a map with few initial elements 
map<string, int> mapp = {
                            {"batman", 100}, 
                            {"superman", 500}, 
                            {"shaktiman", 250}
                        };     


cout << mapp.size() << endl;                               // number of enteries in myMap
mapp["spiderman"] = 150;                                   // add entry to the map

int bmMarks = mapp.at("batman");                           // get a value in map corresponding to a key
int smMarks = mapp["superman"];

mapp.erase("superman");                                    // erase an entry from the map
mapp.clear();                                              // remove all the enteries from the map

// check whether map is empty
if(mapp.empty()){
    ...
}

// search for an element
if (mapp.find("shaktiman") != mapp.end()) {
    // It means key: "shaktiman" exists in the map
}

// iterate over all elements
for(map<string, int>::iterator it = mapp.begin(); it != mapp.end(); it++) {
    cout << *it << endl;
    cout << it->first << endl;
    cout << it->second << endl;
}
  

Last updated