boost lambda - Copy map to vector -
I have to copy some elements from a stud :: map into vector. It should work in this loop:
typedef int first; Typedef zero * second; Std :: map & lt; First, second & gt; Map; // fill map std :: vector & lt; Seconds & gt; MVec; (Std :: map & lt; First, second & gt; :: const_iterator = this map.begin (); it! = Map.end (); ++ it) {If (this-> First% 2 = = 0) {MVec.push_back (it-> second); }}
Since I do not want to use any type of factories, but instead I want to use: lambda, I tried to use std :: copy , But it can not get right.
std :: copy (map.begin (), map.end (), std :: back_inserter (mVec) dam (& std :: map & lt; int, void * & gt; ; :: value_type :: first, _1)% 2 == 0);
I'm new to Lambda expression, and I do not understand how to use them correctly I did not get any useful results on Google or StackOverflow.
What you will need in STL will be a transform-ef algorithm. Then you have to write:
transform_if (mymap.begin (), mymap.end (), back_incenter (myvec), bind (& std :: map & lt; first, second & gt; : Value_type :: second, _1), (bind (& std :: map & lt; first, second & gt; :: value_type :: first, _1)% 2) == 0);
The code for transform_if is taken and it is:
template & class; InputIterator, Class Output Iterator, Class UnaryFunction, Class Preddate & gt; Output Eater Transform_If (InputItterator First, Finally InputItterator, Output Etherator Result, UniFunction F, Preddict Pre) {for (! First! = Last; ++ first) {if (pred (* first)) Results ++ = f ( * First); } Return results; }
I think there is no other way to do both the steps (conversion and conditional copy) using the STL algorithm only once.
Comments
Post a Comment