Boost logo

Boost Users :

From: Gottlob Frege (gottlobfrege_at_[hidden])
Date: 2006-09-19 23:24:44


On 9/19/06, Flex Ferrum <flex_ferrum_at_[hidden]> wrote:
>
> Hello, Alexander Shyrokov,
>
> You wrote:
> > I want to convert values according to a map before inserting it into the
> > list. What is a proper way to do that?
> > I have this:
>
> > map<int,int> m;
> > vector<int> a,b;
>
> > for(vector<int>const_iterator i=a.begin();i!=a.end();++i)
> > b.push_back(m[*i]);
>
> > I could use std::copy with std::back_inserter to add elements to b, but
> > how do I convert it first? Should I write my own version of
> > back_inserter for that?
>
> > copy(a.begin(),a.end(),my_back_inserter(b,m));
>
> > Thanks.
>
> Try this:
>
> #include <boost/lambda/lambda.hpp>
>
> using namespace boost;
> using namespace boost::lambda;
>
> //...
> //...
>
> map<int,int> m;
> vector<int> a,b;
> std::transform(a.begin(), a.end(), std::back_inserter(b), var(m)[_1]);

I would be really interested to know which of the various styles people find
the best.

Tony



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net