Answers for "map operations in c++"

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 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 cpp

#include <map>

// empty map container 
map<int, int> gquiz1; 
  
// insert elements in random order 
gquiz1.insert(pair<int, int>(1, 40));
Posted by: Guest on October-01-2020

Browse Popular Code Answers by Language