|
Boost Users : |
From: Peter Dimov (pdimov_at_[hidden])
Date: 2006-09-19 10:00:34
Alexander Shyrokov 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));
One option is to use
std::transform( a.begin(), a.end(), std::back_inserter( b ), F );
where F(x) returns m[x].
boost::bind( &std::map<int, int>::operator[], &m, _1 ) can give you one such
F, or you could write your own. I had the half-baked idea to propose
std::map<>::operator() once since a map is-a function (and vice versa) by
definition, but never got to it.
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