I have a const std::map< int, std::string> initialized like so:
const std::map< int, std::string > firstMap = {
    { 1, "First" },
    { 2, "Second"}
};
Then I want to make another const std::map which uses the first map as part of its initial values and also has extends the original data. So it would be something similar to this I guess:
const std::map< int, std::string > secondMap = {
    { <firstMap>},
    { 3, "Third"}
};
so that the secondMap has the three pairs. Is this possible?
EDIT: Also the maps are declared extern.
No, std::map doesn't have the right constructor for this. What you can do however is use a lambda to initialize the variable in place.
const auto secondMap = [&firstMap] {
    std::map<int, std::string> map(firstMap);
    map[3] = "Third";
    return map;
}();
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With