Answers for "stl map"

C++
2

c++ map iterator

#include <iostream>
#include <map>
 
int main() {
  std::map<int, float> num_map;
  // calls a_map.begin() and a_map.end()
  for (auto it = num_map.begin(); it != num_map.end(); ++it) {
    std::cout << it->first << ", " << it->second << 'n';
  }
}
Posted by: Guest on March-08-2020
9

map c++

#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;  
  	}
}
Posted by: Guest on August-22-2020
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