|
Boost Users : |
From: Christian Rössel (christian.roessel_at_[hidden])
Date: 2006-09-08 05:03:32
Baranowski, Daniel schrieb:
> Hey all!
>
> I've really enjoyed using boost::bind for calling member functions and
> such. I wish I had found this earlier!
>
> Anyhow, I'm trying to write for_each statement that only passes the
> object to a function if certain conditions are met. Unfortunately, I'm
> not well versed enough in boost::bind (and I've never used the lambda
> library) so I'm not sure how to do this! Can someone lend me a hand? I
> couldn't find anything in the mail archives.
>
> If I were writing a regular for-loop, this is how it would look:
> vector<MyObjClass> myVector;
> myVector.push_back(MyObjClass(2));
> myVector.push_back(MyObjClass(3));
> myVector.push_back(MyObjClass(3));
> myVector.push_back(MyObjClass(5));
> myVector.push_back(MyObjClass(6));
>
> vector<MyObjClass>::iterator end = myVector.end();
> vector<MyObjClass>::iterator itr;
> for(itr = myVector.begin(); itr != end; ++itr)
> {
> if((*itr).GetValue() == 3)
> ProcessingFunction(*itr);
> }
>
> And here's the for_each that just passes every object into the
> processing function:
> // From inside a function in a class called ThisClass
> vector<MyObjClass> myVector;
> myVector.push_back(MyObjClass(2));
> myVector.push_back(MyObjClass(3));
> myVector.push_back(MyObjClass(3));
> myVector.push_back(MyObjClass(5));
> myVector.push_back(MyObjClass(6));
>
> vector<MyObjClass>::iterator begin = myVector.begin();
> vector<MyObjClass>::iterator end = myVector.end();
>
> for_each(begin, end,
> boost::bind(&ThisClass::ProcessingFunction, this, _1));
>
> So I guess the question is, how do I add the condition into the
> for_each? I know there's got to be a way!
Hi Daniel,
I think you can solve your problem with a boost::filter_iterator which
implements your condition. Then, pass this iterator(s) to for_each.
Have fun,
Christian
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net