Insert std :: initializer_list into std :: map

I have a way like this:

std::map<std::string, int> container;

void myMap(std::initializer_list<std::pair<std::string, int>> input)
{
    // insert 'input' into map...
}

I can call this method as follows:

myMap({
    {"foo", 1}
});

How can I convert my own argument and paste into the map?

I tried:

container = input;

container(input);

But do not work, because the card parameter std::initializer_listis not there std::pair.

Thanks to everyone.

+3
source share
2 answers
container.insert(input.begin(), input.end());

If you want to replace the contents of the card. at first container.clear();.

+8
source

Your problem is that the value_type of std :: map <std :: string, int> is not std :: pair <std :: string, Int>. This is std :: pair <const std :: string, int>. Pay attention to the constant on the key. This works great:

std::map<std::string, int> container;

void myMap(std::initializer_list<std::pair<const std::string, int>> input) {
    container = input;
}

, std:: copy, _type . , , , , myMap, otherGuysMap:).

+6

All Articles