Boost logo

Boost Users :

From: Eric Twietmeyer (etwietmeyer_at_[hidden])
Date: 2003-08-25 10:52:13


Hello,

I'm been having great fun getting to know and starting to use the lambda
library over the past month or so. I can't believe how well the optimizing
compiler I have (Vc7.1) is able to convert these lambda expressions used in
for_each or find_if into tight loops.

However, there I things I'm certain are possible, but I haven't been able to
figure out. More examples of using the library than are found in the doc
would be extremely useful.

For instance, how do I do the equivalent of the following using a single
for_each with suitable lambda expression as operator?

using namespace boost::lambda;
std::vector<int> vec1;
std::vector<int> vec2;

struct tOp
{
   std::vector<int>& m_vec;
   tOp( std::vector<int>& vec ) : m_vec( vec ) {}
   void operator()( int entry )
   {
      std::vector<int>::iterator it = std::find_if( m_vec.begin(),
m_vec.end(), _1 == entry );
      if ( it != m_vec.end() ) m_vec.erase( it );
   }
};

// How to use lambda expression here for third arg?
std::for_each( vec1.begin(), vec1.end(), tOp( vec2 ));

Basically, I have two lists (vectors) that overlap. I want to erase all
elements from the one vector from the other vector, i.e. determine if the
element from vec1 exists in vec2, and if so erase it from vec2.

If this is possible and relatively straight forward using a lambda
expression, I would love to see an example.

Thanks in advance,

Eric Twietmeyer

PS It looks like something of this sort can be done with std::set_difference
if one outputs into a third vector, but for argument's sake, let's assume I
don't want to introduce a third vector.

---------------------------------
Do you Yahoo!?
Yahoo! SiteBuilder - Free, easy-to-use web site design software

[Non-text portions of this message have been removed]


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