c++ - boost::bind to concatenate strings in std::transform -
i trying concatenate 2 string using boost::bind inside std::transform
assuming class has 2 methods 2 strings (first , second) , conatiner vector of strings, trying follows
struct myclass { std::string getfirststring() {return string1} std::string getsecondstring() {return string2} private: std::string string1; std::string string2; } myclass myobj; std::vector<string > newvec; std::vector<myobj> oldvec; std::transform (oldvec.begin(), oldvec.end(), std::back_inserter(newvec), boost::bind(&std::string::append,boost::bind(&getfirststring, _1),boost::bind(&getsecondstring, _1 ) ) );
but, error saying
error: cannot call member function 'virtual const getsecondstring() ' without object
what missing here?
you have 2 problems.
the first you're taking address of member function incorrectly. have specify class, i.e. boost::bind(&myclass::getfirststring, _1)
.
the second you're trying bind std::string::append
, modifies object it's called on. want operator +
. since can't bind directly, use std::plus<std::string>
instead. should like:
std::transform(oldvec.begin(), oldvec.end(), std::back_inserter(newvec), boost::bind(std::plus<std::string>(), boost::bind(&myclass::getfirststring, _1), boost::bind(&myclass::getsecondstring, _1) ) );
or can use boost.lambda instead. , while you're @ it, use boost.range, it's awesome.
namespace rg = boost::range; namespace ll = boost::lambda; rg::transform(oldvec, std::back_inserter(newvec), ll::bind(&myclass::getfirststring, ll::_1) + ll::bind(&myclass::getsecondstring, ll::_1));
Comments
Post a Comment