lundi 13 juillet 2020

Why the content of the string in initializer_list is empty?

I am using a map<string,initializer_list>, but I notice that the initializer_list contains empty string, i.e. {"Red","Green","Blue"} will be {"","",""}. By replacing the initializer_list by a vector, the problem will be solved. Why did I have this behavior? Thank you!

std::map<std::string, std::initializer_list<std::string>> container = { {"Color",{"Red","Green","Blue"}},{"Car",{"BMW","Seat"}} };
for (const auto& item : container) {
    std::cout << item.first << std::endl;
    for (const auto& option : item.second)
    {
        std::cout << option << std::endl; //  option is always "", it should be "Red" or "Green" or "BMW" ...
    }
}

Aucun commentaire:

Enregistrer un commentaire