Answers for "how declare map in cpp"

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

Browse Popular Code Answers by Language