Boost logo

Boost Users :

Subject: Re: [Boost-users] A question on for_each() and placeholder.Can any one with kindness help me?
From: Steven Watanabe (watanabesj_at_[hidden])
Date: 2009-06-27 21:33:55


AMDG

fmingu wrote:
> By using Dev-C++, I used lambda expression, I want to know that if I white:
> typedef std::vector<int> vect1;
> std::vector<vect1> vect2;
> vect2 vect2Instance;
> then
> std::for_each(vect2Instance.begin(),vect2Instance.end(),
> std::for_each(_1.begin(),_1.end(),_1+=2));
> is legal or not? And if legal ,Does the first two _1's mean a number
> in vect2 (i.e. a vect1) and the last _1 is a number in vect1(i.e. a int)?
> Can any one with kindness help me?
>

No. It isn't legal. std::for_each cannot return a function object.
Boost.Lambda provides lazy wrappers for some algorithms.

Here's a complete sample. Note that you can't use boost::lambda::call_begin
because it always returns a const_iterator. (Which I will fix shortly).

#include <boost/lambda/lambda.hpp>
#include <boost/lambda/algorithm.hpp>
#include <boost/lambda/bind.hpp>
#include <boost/range/iterator.hpp>
#include <boost/range/begin.hpp>
#include <boost/range/end.hpp>
#include <vector>

using namespace boost::lambda;

struct begin {
template<class Args>
struct sig {
typedef typename boost::range_iterator<
typename boost::tuples::element<1, Args>::type
>::type type;
};
template<class T>
typename boost::range_iterator<T>::type operator()(T& t) const {
return(boost::begin(t));
}
};

struct end {
template<class Args>
struct sig {
typedef typename boost::range_iterator<
typename boost::tuples::element<1, Args>::type
>::type type;
};
template<class T>
typename boost::range_iterator<T>::type operator()(T& t) const {
return(boost::end(t));
}
};

int main() {
std::vector<std::vector<int> > vec(4, std::vector<int>(4));
std::for_each(vec.begin(), vec.end(),
bind(ll::for_each(), bind(begin(), _1), bind(end(), _1), protect(_1 += 2)));
}

In Christ,
Steven Watanabe


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