Monday, 15 August 2011

c++ - std::map initialization with a std::vector -



c++ - std::map initialization with a std::vector -

i want initialize std::map object keys contained in std::vector object.

std::vector<char> mykeys { 'a', 'b', 'c' ]; std::map<char, int> mymap;

how can without loop?

and can add together default value int?

without explicit loop:

std::transform( std::begin(mykeys), std::end(mykeys), std::inserter(mymap, mymap.end()), [] (char c) {return std::make_pair(c, 0);} );

demo.

a range-based loop far more sexy though, utilize if possible:

for (auto c : mykeys) mymap.emplace(c, 0);

c++ c++11 stl

No comments:

Post a Comment