Example 1: map declaration c++
#include <map>
#include <string>
std::map<int, std::string> map_;
map_[1] = "mercury";
map_[2] = "mars";
map_.insert(std::make_pair(3, "earth"));
return map_[2];
Example 2: map c++
#include <iostream>
#include <string>
#include <map>
using std::string;
using std::map;
int main() {
map<string, int> myMap = {
{"one", 1},
{"two", 2},
{"three", 3}
};
for (auto& iter: myMap) {
std::cout << iter.first << ": " << iter.second << std::endl;
}
}
Example 3: map in cpp
#include <map>
map<int, int> gquiz1;
gquiz1.insert(pair<int, int>(1, 40));
Example 4: map of maps c++
map <typename,map<typename,typename>> mp;
map[key1][key2]=values
Example 5: basic ex of maps in c++
#include <iostream>
#include <iterator>
#include <map>
using namespace std;
int main()
{
map<int, int> marks;
marks.insert(pair<int, int>(160, 42));
marks.insert(pair<int, int>(161, 30));
marks.insert(pair<int, int>(162, 40));
marks.insert(pair<int, int>(163, 50));
marks.insert(pair<int, int>(164, 31));
marks.insert(pair<int, int>(165, 12));
marks.insert(pair<int, int>(166, 34));
map<int, int>::iterator itr;
cout << "nThe map marks is : n";
cout << "ROLL NO.tMarksn";
for (itr = marks.begin(); itr != marks.end(); ++itr) {
cout << itr->first
<< "t t" << itr->second << 'n';
}
cout << endl;
return 0;
}
Example 6: map in cpp
#include <map>
int main(){
std::map <int,int> myMap;
myMap[0] = 1;
myMap[1] = 2;
myMap[2] = 3;
myMap[3] = 4;
}