c++ declare map in constructor code example

Example 1: how to initialize map in c++

std::map<std::string,std::string> my_map = {
  {"key1","value1"},
  {"key2","value2"}
};

Example 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"

Tags:

Cpp Example