C++ Multidimensional string array initialization (std::map) -
i have special array of strings in c++:
#include <map> #include <string> std::map<std::string, std::map<int, std::string>> oparam; oparam["pa"][0] = "a"; oparam["pa"][1] = "b"; oparam["pa"][2] = "c"; oparam["pb"][0] = "x"; oparam["pb"][1] = "y"; oparam["pb"][2] = "z";
but initialize initialization list, this:
std::map<std::string, std::map<int, std::string>> oparam{ { "pa", { "a", "b", "c" } }, { "pb", { "x", "y", "z" } }, };
but giving me error. missing brackets?
if integers acting keys in internal map going contiguous, use vector instead:
std::map<std::string, std::vector<std::string>> oparam;
with that, initialization you've given should work.
if continue use std::map
instead, you'll have couple of things. first, supports sparse keys, you'll need specify key each string want insert. second, you'll need enclose items insert 1 map in braces, this:
std::map<std::string, std::map<int, std::string>> oparam { { "pa", { { 0, "a" }, { 1, "b" }, { 2, "c" } } }, { "pb", { { 0, "x" }, { 1, "y" }, { 2, "z" } } } };
Comments
Post a Comment