Boost logo

Boost Users :

From: Baranowski, Daniel (dbaranowski_at_[hidden])
Date: 2006-09-08 13:45:05


> From: "Andriy Tylychko
> > Baranowski, Daniel
>
> std::for_each(myVector.begin(), myVector.end(),
> boost::lambda::if_then(boost::lambda::_1 == 3, [...]));
>
> Best regards,
> Andriy Tylychko

Thanks Andriy! That definitely got me on the correct track! It still
took me awhile, but I got it! It looks so simple now. Anyhow, since I'm
sure I'm not the only person that will ever have this problem when
learning to use bind and lambda, here is a sample program that I wrote
in vc8 to figure out the solution:

#include <vector>
#include <iostream>
#include <conio.h>

#include <boost/lambda/lambda.hpp>
#include <boost/lambda/if.hpp>
#include <boost/lambda/bind.hpp>
#include <algorithm>

using namespace std;
using namespace boost;

using boost::lambda::_1;
using boost::lambda::if_then;

struct MyObjectClass
{
   MyObjectClass(int Data)
   : MyData(Data)
   {
   };

   int GetData() const
   {
      return MyData;
   };

   int MyData;
};

class TestClass
{
      typedef vector<MyObjectClass> t_ObjVector;
      t_ObjVector myVector;

      void PrintObj(const MyObjectClass & MyObj)
      {
         cout << MyObj.GetData() << endl;
      }

      void PrintInt(int MyInt)
      {
         cout << MyInt << endl;
      }

   public:
      TestClass()
      {
         myVector.push_back( MyObjectClass(1) );
         myVector.push_back( MyObjectClass(2) );
         myVector.push_back( MyObjectClass(2) );
         myVector.push_back( MyObjectClass(3) );
         myVector.push_back( MyObjectClass(3) );
         myVector.push_back( MyObjectClass(4) );
      }

      void RunTest()
      {
         // regular for loop
         t_ObjVector::iterator end = myVector.end();
         t_ObjVector::iterator itr = myVector.begin();
         for(itr; itr != end; ++itr)
         {
            if((*itr).GetData() == 3)
               PrintObj(*itr);
         }
         
         // for_each using lambda concepts
         for_each(myVector.begin(), myVector.end(),
            if_then(bind(&MyObjectClass::GetData, _1) == 3,
               bind(&TestClass::PrintObj, this, _1)
            )
         );
      }
};

int main (int argc, char **argv)
{
   TestClass Test;

   // Run the test
   Test.RunTest();

   getch();

   return 0;
}

Thanks again for the 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.
**********************************************************************************************


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