# Map

```
// case: keys in map is of type int
struct KeyComparator {
    bool operator()(int lhsKey, int rhsKey) const {
        return lhsKey > rhsKey;
    }
};

// case: keys in map is of type string
struct KeyComparator {
    bool operator()(string lhsKey, string rhsKey) const {
        return lhsKey > rhsKey;
    }
};
```

```cpp
#include <map>  

map<string, int> mapp;                                     // create a map of string: {integer}
map<string, int, KeyComparator> 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;
}
  
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://blog.gomchik.com/tech/c++/map.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
