Home > Software engineering >  Having trouble accessing a list value inside a map | C
Having trouble accessing a list value inside a map | C

Time:06-06

I've made a map that has string keys with either string values or list values. It gives me no syntax errors and has worked fine for me so far until I've tried accessing the list value. I've tried setting it as a variable, using auto, and no go. Don't know what else to try, any help is appreciated.


using map = std::map<std::string, std::variant<std::string, std::list<std::string>>>; 
std::list<std::string> TagList{ "Paid", "Needs invoice" }; 


 map m{ {"TIME",TimeHourAndMin}, {"HEADER","Title"},{"TAGS", TagList},{"CONTENT", "Hey this is content!"} };

// E0135 class "std::variant<std::string, std::list<std::string, std::allocator<std::string>>>" has no member "begin"   
// E0135 class "std::variant<std::string, std::list<std::string, std::allocator<std::string>>>" has no member "end"
for (auto it = m["TAGS"].begin(); it != m["TAGS"].end(); it  ) {


        }


CodePudding user response:

The problem is that m["TAGS"] is of type std::variant and so doesn't have a begin method. Since you want to get the list out of your variant you can use the std::get function to do that

auto it = std::get<std::list<std::string>>(m["TAGS"]).begin();
  •  Tags:  
  • c
  • Related