2010-01-04 6 views
1

for 루프를 사용하는 코드를 가지고 있는데 transform 또는 적어도 for_each 대신 사용하고 싶지만 어떻게 볼 수는 없습니다.boost :: function objects의 컨테이너에있는 STL 알고리즘

typedef std::list<boost::function<void(void) > CallbackList; 
CallbackList callbacks_; 

//... 
for(OptionsMap::const_iterator itr = options.begin(); itr != options.end(); ++itr) 
{ 
    callbacks_.push_back(boost::bind(&ClassOutput::write_option_,this,*itr)); 
} 

나중에 코드에서이 nullary 함수 개체 컬렉션을 호출하려고합니다. 나는 for 루프도 여기에 사용하고 있으며 for_each를 어떻게 든 사용할 수 있어야한다. 당신이 당신이 호출하는 펑터를 필요로하기 때문에 당신이 자체에 바인드를 호출 할 필요가 있다고 생각,

typedef boost::function<void(void)> NullaryFunc; 
for_each(callbacks_.begin(),callbacks_.end(),boost::bind(&NullaryFunc::operator(),_1)); 

답변

2

은 하나의 변환 호출에 모든이 작업을 수행하려면 :

for(CallbackList::iterator itr = callbacks_.begin(); itr != callbacks_.end(); ++itr) 
{  
    (*itr)(); 
} 
+0

하나의 변화와 함께 작동 : 대신 펑터 클래스의 람다를 사용하여 C++ 0X에서

struct GetFunc { ClassOutput *obj; boost::function<void(void) > operator()(const OptionsMap::value_type &v) { return boost::bind(&ClassOutput::write_option_, obj, v); } GetFunc(ClassOutput *obj) : obj(obj) {} }; transform(options.begin(), options.end(), back_inserter(callbacks_), GetFunc(this)); 

. operator()의 인수는 OptionsMap :: value_type이어야합니다. 감사. –

+0

고정되었습니다 (15 자). –

3

나는 2 부 알아낼 관리 boost : bind. 그게 내가 시도한 적이없는 것입니다. 이런 (테스팅되지 않은) 뭔가에 정착하겠습니까?

transform(options.begin(), options.end(), back_inserter(callbacks_), 
    [this](const OptionsMap::value_type &v) { 
     return boost::bind(&ClassOutput::write_option_, this, v); 
    } 
);