Answers for "how does a map work in c++"

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

Browse Popular Code Answers by Language