c++ maps code example
Example 1: c++ map iterator
#include <iostream>
#include <map>
int main() {
std::map<int, float> num_map;
for (auto it = num_map.begin(); it != num_map.end(); ++it) {
std::cout << it->first << ", " << it->second << '\n';
}
}
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 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 4: map of maps c++
map <typename,map<typename,typename>> mp;
map[key1][key2]=values
Example 5: map in cpp
#include <map>
int main(){
std::map <int,int> myMap;
myMap[0] = 1;
myMap[1] = 2;
myMap[2] = 3;
myMap[3] = 4;
}