Answers for "map in c++"

C++
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
1

map c++

//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;
}
Posted by: Guest on April-30-2021
2

map declaration c++

//assuming your variables are called : variables_
#include <map>
#include <string>

std::map<int, std::string> map_;
map_[1] = "mercury";
map_[2] = "mars";
map_.insert(std::make_pair(3, "earth"));
//either synthax works to declare a new entry

return map_[2]; //here, map_[2] will return "mars"
Posted by: Guest on July-12-2020
1

map in cpp

#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';
  }
}
Posted by: Guest on June-15-2021
1

map at function c++

// map::at
#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;
}
Posted by: Guest on May-07-2021
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

Browse Popular Code Answers by Language