map in cpp
#include <iostream>
#include <string>
#include <map>
using std::string;
using std::map;
int main() {
// A new map
map<string, int> myMap = { // This equals sign is optional
{"one", 1},
{"two", 2},
{"three", 3}
};
// Print map contents with a range-based for loop
// (works in C++11 or higher)
for (auto& iter: myMap) {
std::cout << iter.first << ": " << iter.second << std::endl;
}
}
4.1
10
#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;
//to find the value of a key
//A.find(key)
//to delete the key
//A.erase(key)
map<char, int> cnt;
string x = "Sumant Tirkey";
for(char c:x){
cnt[c]++;//map the individual character with it's occurance_Xtimes
}
//see how many times a and z occures in my name
cout<< cnt['a']<<" "<<cnt['z']<<endl;
}
int main() {
mapDemo();
return 0;
}
Thank you!
10
0
4.6
5
#include<bits/stdc++.h>
using namespace std;
int main()
{
map<string,int>mapa;
for(int i=0;i<10;i++){
int x;
string s;
cin>>x>>s;
mapa.insert({s,x});
}
for(auto [x,y]:mapa){
cout<<x<<" "<<y<<'\n';
}
}
Thank you!
5
0
4.38
8
//Since c++17
for (auto& [key, value]: myMap) {
cout << key << " has value " << value << endl;
}
//Since c++11
for (auto& kv : myMap) {
cout << kv.first << " has value " << kv.second << endl;
}
Thank you!
8
0
3.83
6
#include <map>
int main(){
//new map
std::map <int,int> myMap;
myMap[0] = 1;
myMap[1] = 2;
myMap[2] = 3;
myMap[3] = 4;
}
Thank you!
6
0
0
4
#include <map>
// empty map container
map<int, int> gquiz1;
// insert elements in random order
gquiz1.insert(pair<int, int>(1, 40));
Thank you!
4
0
Are there any code examples left?
New code examples in category C++