How can I construct a std::map<std::string, Foo>
from a std::vector<std::pair<std::string, Foo> >
? It seems the std::map can be constructed from a input iterator.
UPDATE
Incidentally, I need to convert the strings in the vector to their lower case forms when adding them to the map. This is because I wish to use the map to get a sorted version of whats in the vector.
Every standard library container can be constructed from an iterator range. In your case:
If you want to add the lower-case versions of the strings, you need to pass the values through a transforming iterator. Unfortunately that’s not included in standard C++ but it’s rather straightforward to implement. Boost also includes a version:
Here’s a complete, running demonstration