In our last exciting episode Eric Twietmeyer wrote: It should be doable, but it's not the easiest thing to do. Look at the section 5.11. on nesting STL algorithm invocations: http://www.boost.org/libs/lambda/doc/ar01s05.html#sect:nested_stl_algorithms Jaakko
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.