c++ - Print Vector elements using Boost.Bind -
i need print values inserted in vector using boost.bind. please find code snippet below:
please let me know missing here?
class test { int i; public: test() {} test(int _i) { = _i; } void print() { cout << << ","; } }; int main() { std::vector<test> vtest; test w1(5); test w2(6); test w3(7); vtest.push_back(w1); vtest.push_back(w2); vtest.push_back(w3); std::for_each(vtest.begin(), vtest.end(),boost::bind(boost::mem_fn(&test::print), _1, ?)); // how print vector elements here? }
you can without boost this
#include <iostream> #include <vector> #include <algorithm> #include <functional> class test { int i; public: test() { } test(int _i) { = _i; } void print() const { std::cout << << std::endl; } }; int main() { std::vector<test> vtest; test w1(5); test w2(6); test w3(7); vtest.push_back(w1); vtest.push_back(w2); vtest.push_back(w3); // use lambda std::for_each(vtest.begin(), vtest.end(), [&](const test& t){ t.print(); }); // use std::bind std::for_each(vtest.begin(), vtest.end(), std::bind(&test::print, std::placeholders::_1)); return 0; }
Comments
Post a Comment