Использование boost::lambda
От:
Аноним
Дата: 15.04.10 14:40
Оценка:
Всем привет.
Есть код:
int f1( int i )
{
return i;
}
void exec( boost::function<int (int )> f, int i )
{
std::cout << f( i ) << "\n" ;
}
int main()
{
std::vector<boost::function<int (int )> > s;
s.push_back( f1 );
// ......
// ......
// s.push_back( f10 );
int param = 7;
std::for_each( s.begin(), s.end(), boost::bind( exec, _1, param ) );
return 0;
}
Вопрос такой: как избавиться от ф-ии exec? Ведь наверняка же можно через boost::lambda!
Спасибо.
Re: Использование boost::lambda
Здравствуйте, Аноним, Вы писали:
std::for_each( s.begin(), s.end(), std::cout << boost::lambda::bind(&boost::function<int (int)>::operator(), boost::lambda::_1, param));
Re[2]: Использование boost::lambda
Здравствуйте, placement_new, Вы писали:
уж совсем точно
std::for_each( s.begin(), s.end(), std::cout << boost::lambda::bind(&boost::function<int (int)>::operator(), boost::lambda::_1, param) << '\n');
Re: Использование boost::lambda
От:
_nn_
Дата: 15.04.10 16:08
Оценка:
Здравствуйте, Аноним, Вы писали:
А>Всем привет.
А>Есть код:
А>А>int f1( int i )
А>{
А> return i;
А>}
А>void exec( boost::function<int (int )> f, int i )
А>{
А> std::cout << f( i ) << "\n" ;
А>}
А>int main()
А>{
А> std::vector<boost::function<int (int )> > s;
А> s.push_back( f1 );
А> // ......
А> // ......
А> // s.push_back( f10 );
А> int param = 7;
А> std::for_each( s.begin(), s.end(), boost::bind( exec, _1, param ) );
А> return 0;
А>}
А>
А>Вопрос такой: как избавиться от ф-ии exec? Ведь наверняка же можно через boost::lambda!
А>Спасибо.
Ну или через BOOST_FOREACH:
typedef boost::function<int (int )> intint_f;
std::vector<intint_f> s;
BOOST_FOREACH(intint_f& v, s)
{
exec(v, param);
}
Re: Использование boost::lambda
Здравствуйте, Аноним, Вы писали:
А>Всем привет.
skip
А>Вопрос такой: как избавиться от ф-ии exec? Ведь наверняка же можно через boost::lambda!
А>Спасибо.
#include <boost/lambda/lambda.hpp>
#include <boost/lambda/bind.hpp>
int main()
{
std::vector< boost::function<int (int )> > s;
s.push_back( f1 );
int param = 7;
using boost::lambda::bind;
using boost::lambda::_1;
std::for_each( s.begin(), s.end(), std::cout << bind( _1, param ) << "\n" );
return 0;
}
Re: Использование boost::lambda
От:
zaufi
Дата: 16.04.10 00:15
Оценка:
+1
Здравствуйте, Аноним, Вы писали:
А>Вопрос такой: как избавиться от ф-ии exec? Ведь наверняка же можно через boost::lambda!
нафиг не нужна тут boost::lambda! (она гаразда тяжелее обычных bind'ов)
курим доки (не по диагонале)
здесь
#include <iostream>
#include <algorithm>
#include <vector>
#include <boost/function.hpp>
#include <boost/bind/apply.hpp>
#include <boost/bind.hpp>
using namespace std;
int f1( int i )
{
return i;
}
int main()
{
std::vector<boost::function<int (int )> > s;
s.push_back( f1 );
// ......
// ......
// s.push_back( f10 );
int param = 7;
std::for_each( s.begin(), s.end(), boost::bind( boost::apply<int >(), _1, param ) );
return 0;
}
А>Спасибо.
Re: Использование boost::lambda
От:
ariets
Дата: 16.04.10 08:19
Оценка:
Спасибо всем.
Не знал, что в lambda есть свой bind.
Re: Использование boost::lambda
Во времена, когда основные популярные компиляторы полностью или частично поддерживают фичи C++0x, стыдно пользоваться boost::lambda и boost::function:
#include <functional>
#include <iostream>
#include <vector>
#include <algorithm>
int f1( int i )
{
return i;
}
typedef std::function<int (int )> Func;
int main(int argc, char argv[])
{
std::vector<Func> s;
s.push_back( f1 );
int param = 7;
std::for_each(
s.begin(),
s.end(),
[=](const Func &func) {
std::cout << func(param) << "\n" ;
}
);
return 0;
}
Пока на собственное сообщение не было ответов, его можно удалить.
Удалить