On the following code, how can i rewrite the for loop by using a std::for_each instruction.
I tried to use boost::lambda::_1, boost::bind, but I could not make it working.
#include <vector>
#include <iostream>
#include <cstring>
#include <cstdlib>
int main()
{
std::vector<int(*)(const char*)> processors;
processors.push_back(std::atoi);
processors.push_back(reinterpret_cast<int(*)(const char*)>(std::strlen));
const char data[] = "1.23";
for(std::vector<int(*)(const char*)>::iterator it = processors.begin();
it != processors.end(); ++it)
std::cout << (*it)(data) << std::endl;
}
Any hint to help me solve this problem are welcome.
EDIT: Solution
#include <vector>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <boost/function.hpp>
#include <boost/lambda/lambda.hpp>
#include <boost/lambda/bind.hpp>
int main()
{
std::vector<boost::function<int(const char*)> > processors;
processors.push_back(std::atoi);
processors.push_back(std::strlen);
const char data[] = "1.23";
namespace bl = boost::lambda;
std::for_each(processors.begin(), processors.end(),
std::cout << bl::bind(bl::_1, data) << "\n");
}
boost::function<int(const char*)>instead ofint(*)(const char*). It will handle the return-type conversion forstd::strlen(fromsize_ttoint).boost::bind(std::cout << boost::bind(_1, data) << "\n"andstd::cout << (int(*)(const char*))boost::lambda::_1(data) << "\n"