How to use bind/lambda in a conditional for_each?
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! Thanks for any help! Daniel ********************************************************************************************** Disclaimer - This email and any files transmitted with it are proprietary and may contain privileged or copyright information. You must not present this message to another party without gaining permission from the sender. If you are not the intended recipient you must not copy, distribute or use this email or the information contained in it for any purpose other than to notify us. If you have received this message in error, please notify the sender immediately, and delete this email from your system. We do not guarantee that this material is free from viruses or any other defects although due care has been taken to minimize the risk. eSafe scanned this email for viruses, vandals and malicious contentAny views expressed in this message are those of the individual sender, except where the sender specifically states them to be the views of LSI. **********************************************************************************************
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
Baranowski, Daniel
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(6));
vector<MyObjClass>::iterator end = myVector.end(); vector<MyObjClass>::iterator itr; for(itr = myVector.begin(); itr != end; ++itr) { if((*itr).GetValue() == 3) ProcessingFunction(*itr); }
std::for_each(myVector.begin(), myVector.end(), boost::lambda::if_then(boost::lambda::_1 == 3, [...])); Best regards, Andriy Tylychko, telia@vichnavich.com www.vichnavich.com
participants (3)
-
Andriy Tylychko (mail.ru)
-
Baranowski, Daniel
-
Christian Rössel