Move insert range / rvalue iterators?

I have a vector that contains values that I want to insert to an unordered_map.

1
2
3
std::unordered_map<std::string, int> map;
std::vector<std::pair<std::string, int>> vec = { {"a", 1}, {"b", 2} };
map.insert(vec.begin(), vec.end());

The vector is not needed after this so I would like the pairs to be moved from the vector to the map, rather than copied. I know I could loop over the vector and insert each pair, one by one.

1
2
3
4
for (auto& p : vec)
{
	map.insert(std::move(p));
}

But I would like to know if there is something as short as just calling insert.
I found I could use std::move_iterator. I have checked and it now inserts the objects by moving instead of copying.

1
2
map.insert(std::make_move_iterator(vec.begin()), 
	   std::make_move_iterator(vec.end()));

Does anyone know if this is guaranteed to work? I can't see anything that says the insert function has to be implemented in a way that can take advantage of move iterators. The standard just says "some generic algorithms can be called with move iterators to replace copying with moving", and then it gives one example of how you can use it to populate a vector by passing two move iterators to the constructor.

I'm going to mark this thread as solved because it's "good enough" for my use case. If copying happened it just means the code is less efficient, but it would still work.
Topic archived. No new replies allowed.