c++ - Boost lambda function call -
c++ - Boost lambda function call -
i learning boost lambda (not c++0x lambda because guess different). can't find way online phone call fellow member function (and output result) if input parameter phone call object. mean line works:
for_each(vecct.begin(), vecct.end(), cout<<_1<<endl); if vecct vector of int. if vecct vector of myclass, has function called getname homecoming string? neither this:
for_each(vecct.begin(), vecct.end(), cout<<_1->getname()); nor this:
for_each(vecct.begin(), vecct.end(), cout<<*_1.getname()); works.
i searched online many results suggest utilize bind when calling fellow member function. know this
for_each(vecct.begin(), vecct.end(), bind(&myclass::getname, _1); makes me able phone call getname on each object passed int, how can pass output cout? doesn't work:
for_each(vecct.begin(), vecct.end(), cout<<bind(&myclass::.getname, _1);
quite you're mixing placeholders , functions boost::, global, boos::lambda (possibly more, boost::phoenix too).
here's fixed demo: live on coliru
#include <boost/lambda/lambda.hpp> #include <boost/lambda/bind.hpp> #include <iostream> #include <algorithm> #include <vector> #include <string> struct x { int x; std::string accessor() const { homecoming "x[" + std::to_string(x) + "]"; } // know, requires c++11 }; int main() { std::vector<x> v; v.push_back({ 1 }); v.push_back({2}); v.push_back({3}); v.push_back({4}); v.push_back({5}); std::for_each(v.begin(), v.end(), std::cout << boost::lambda::bind(&x::accessor, boost::lambda::_1) << "\n"); } c++ boost boost-lambda
Comments
Post a Comment