Answers for "maps in stl"

C++
2

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;
	//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;
}
Posted by: Guest on December-04-2020
0

maps stl

#include<iostream>
#include<map>
#include<algorithm>

using namespace std;

int main()
{
    map<int,int>map1{{1,2},{2,3},{3,5},{4,7},{5,11}};
    cout<<map1.at(3)<<endl;
    cout<<map1[3]<<endl;
    map1[6];
    for(map<int,int>::iterator it=map1.begin();it!=map1.end();it++)
    {
        cout<<it->first<<" "<<it->second<<endl;
    }
    cout<<endl;
    cout<<"----------------------"<<endl;
    map1.insert(pair<int,int>(7,17));
    map1.insert(make_pair(8,23));

    for(map<int,int>::iterator it=map1.begin();it!=map1.end();it++)
    {
        cout<<it->first<<" "<<it->second<<endl;
    }
    cout<<endl;
    map<int,int>::iterator its=map1.find(2);
    map1.erase(its);
    for(map<int,int>::iterator it=map1.begin();it!=map1.end();it++)
    {
        cout<<it->first<<" "<<it->second<<endl;
    }
    cout<<endl;
    auto itss=map1.begin();
    auto itss2=map1.end();
    map1.erase(itss,itss2);
    for(map<int,int>::iterator it=map1.begin();it!=map1.end();it++)
    {
        cout<<it->first<<" "<<it->second<<endl;
    }
    cout<<endl;

    return 0;
}
Posted by: Guest on June-04-2021

Browse Popular Code Answers by Language