C++/Boost Insert list items into map without manual loop -


in c++ program, have std::list , std::map (although using boost:unordered_map) , know elegant way of inserting elements in list map. key result of method call on elements on in list.

so example have:

std::list<message> messages = *another list elements*; std::map<std::string, message> message_map; 

and want insert elements list map key being message.id(), every message in messages.

is there way without looping on list , doing manually? i can't use c++11 still interested in c++11 solutions interests sake. able use boost.

thank you.

a c++11 solution: can use std::transform transform std::list elements std::map elements:

std::transform(message.begin(), messages.end(),                std::inserter(message_map, message_map.end()),                [](const message& m) { return std::make_pair(m.id(), m); }); 

the equivalent can done c++03 passing function pointer instead of lambda.


Comments

Popular posts from this blog

c++ - Function signature as a function template parameter -

algorithm - What are some ways to combine a number of (potentially incompatible) sorted sub-sets of a total set into a (partial) ordering of the total set? -

How to call a javascript function after the page loads with a chrome extension? -