The first few examples are just a copy of the [introductory examples] from the Boost Lambda Library documentation (http://www.boost.org/doc/html/lambda/using_library.html)
Another source of examples is the set of regression tests that are included in the Boost distribution. These are located at {boost_dir}/libs/lambda/test .
Back to lambda library pages
#include <boost/lambda/lambda.hpp> #include <boost/lambda/bind.hpp> #include <vector> #include <algorithm> using namespace std; using namespace boost::lambda;
list<int> v(10); for_each(v.begin(), v.end(), _1 = 1);
The expression _1 = 1 creates a lambda functor which assigns the value 1 to every element in v.
vector<int*> vp(10); transform(v.begin(), v.end(), vp.begin(), &_1);
The expression &_1 creates a function object for getting the address of each element in v. The addresses get assigned to the corresponding elements in vp.
int foo(int); for_each(v.begin(), v.end(), _1 = bind(foo, _1));
sort(vp.begin(), vp.end(), *_1 > *_2);
In this call to sort, we are sorting the elements by their contents in descending order.
for_each(vp.begin(), vp.end(), cout << *_1 << '\n');
for_each(vp.begin(), vp.end(), cout << constant('\n') << *_1);
vector<double> v; ... double sum = 0; for_each(v.begin(), v.end(), var(sum) += _1)
vector<double> v; ... double (*pFloor)(double) = &floor; for_each(v.begin(), v.end(), _1 = bind(pFloor,_1) );
Trying to do bind(floor,_1) directly doesn't work. You can also cast floor() within the bind call directly:
for_each(v.begin(), v.end(), _1 = bind((double(*)(double))floor,_1) );
struct Thing { void dump() { cout << num << "\n"; } int num; }; vector<Thing> tv; vector<Thing*> tpv; ... for_each(tv.begin(), tv.end(), bind(&Thing::dump, _1)); for_each(tpv.begin(), tpv.end(), bind(&Thing::dump, *_1));
for_each(tv.begin(), tv.end(), bind(&Thing::num, _1) = 42); for_each(tpv.begin(), tpv.end(), bind(&Thing::num, *_1) = 42);
struct Base { virtual string speak()=0; }; struct Derived : public Base { virtual string speak() { return "ook"; } }; vector<Base*> v; ostringstream oss; //for_each(v.begin(),v.end(),oss << bind(&Base::speak,*_1) << "\n"); // gives error: cannot instantiate abstract class Base for_each(v.begin(),v.end(),oss << bind(&Base::speak,_1) << "\n"); // compiles and works correctly - WHY?
Thanks, Tim
If you fix one of these broken examples, please move it out of the "WANTED" section of this page.
// additional includes #include <algorithm> // fill #include <boost/lambda/construct.hpp> typedef vector<int> Row; typedef vector<Row> Matrix; Matrix m(5); // the following line does not work fill(m.begin(), m.end(), bind(constructor<Row>(), 5)); // // this is the desired result // for (Matrix::iterator i = m.begin(); i != m.end(); ++i) { // *i = Row(5); // } // Should perhaps do the following: // fill(m.begin(), m.end(), _1 = bind(constructor<Row>(), 5)); // This will presumably construct empty Rows, then assign a Row // of length five for each empty row and assign it to the empty // row via the assignment operator. Very inefficient. Would appreciate // authoritative clarification and/or better option. (From Elliott)