В следующем коде, как я могу переписать цикл for с помощью инструкции std :: for_each.Я пытался использовать boost::lambda::_1
, boost::bind
, но не мог заставить его работать.
#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;
}
Любые подсказки, которые помогут мне решить эту проблему, приветствуются.
РЕДАКТИРОВАТЬ: Решение
#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");
}