how to use maps in c++ code example
Example 1: map in c++
#include <bits/stdc++.h>
#include <iostream>
#include <map>
using namespace std;
void mapDemo(){
map<int, int> A;
A[1] = 100;
A[2] = -1;
A[3] = 200;
A[100000232] = 1;
map<char, int> cnt;
string x = "Sumant Tirkey";
for(char c:x){
cnt[c]++;
}
cout<< cnt['a']<<" "<<cnt['z']<<endl;
}
int main() {
mapDemo();
return 0;
}
Example 2: map of maps c++
map <typename,map<typename,typename>> mp;
map[key1][key2]=values
Example 3: 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;
}