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

Perl - how to grep a block of text from a file -

delphi - How to remove all the grips on a coolbar if I have several coolbands? -

javascript - Animating array of divs; only the final element is modified -