map and its functions 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 at function c++
#include <iostream>
#include <string>
#include <map>
int main ()
{
std::map<std::string,int> mymap = {
{ "alpha", 0 },
{ "beta", 0 },
{ "gamma", 0 } };
mymap.at("alpha") = 10;
mymap.at("beta") = 20;
mymap.at("gamma") = 30;
for (auto& x: mymap) {
std::cout << x.first << ": " << x.second << '\n';
}
return 0;
}