c++ - How to use a function from <cmath> with Boost.Lambda? -


i try transform elements of vector v log values other arithmetic operations (not in code). how can use boost.lambda achieve that?

as say, there more arithmetic operations, expression boost.bind doesn't work me (too complicated, long, unreadable).

i don't want use c++11 lambdas well. but... change anything?

my code like:

#include <boost/lambda/lambda.hpp> #include <cmath> #include <vector>  void testlambda() {     using namespace boost::lambda;      std::vector<double> v;     v.push_back(1); v.push_back(2); v.push_back(3);      std::transform(v.begin(), v.end(), v.begin(), _1 / 0.5);     // works     std::transform(v.begin(), v.end(), v.begin(), log(_1) / 0.5);   // produces error     //std::transform(v.begin(), v.end(), v.begin(), std::log(_1) / 0.5);   // produces error     //std::transform(v.begin(), v.end(), v.begin(), static_cast<double(*)(double)>(std::log)(_1) / 0.5);   // produces error } 

when try compile code, msvc2010 gives error:

error   1   error c2665: 'log' : none of 3 overloads convert argument types c:\program files (x86)\microsoft visual studio 10.0\vc\include\math.h(120): 'double log(double)' c:\program files (x86)\microsoft visual studio 10.0\vc\include\math.h(527): or       'float log(float)' c:\program files (x86)\microsoft visual studio 10.0\vc\include\math.h(575): or       'long double log(long double)' while trying match argument list '(boost::lambda::placeholder1_type)' 

update 1: don't want write functors it, think have have dozen of them, then?

update 2: able c++11 lambdas, it's not ask for:

   std::transform(v.begin(), v.end(), v.begin(), [](const double & x) { return log(x) / 0.5; }); 

how proper c++11 lamba? msvc2010 has limited support, plain math should work fine.

std::transform(v.begin(), v.end(), v.begin(), [](const double& x) { return log(x); }); 

or old-school solution problem:

struct f {    double operator()(const double& x)    {      return log(x);    } } std::transform(v.begin(), v.end(), v.begin(), f); 

anyways, see no need fancy lambda stuff in code posted since seem modifying vector's elements in-place:

std::vector<double> v; v.push_back(1); v.push_back(2); v.push_back(3); for(const std::vector<double>::iterator = v.begin(); != v.end(); ++it) {   /* fancy math stuff here */   *it = log(*it); } 

this imho cleanest way this. final c++ solution (by far expressive , simplest of every alternative) be:

for(auto&& x : v) {   /* fancy math stuff here */   x = log(x); } 

Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -