Listing 4 : The ubiquitous if_then and its cousin if_.
#include <boost/lambda/lambda.hpp> #include <boost/lambda/if.hpp> int main() { using namespace boost::lambda; std::vector<int> vec; for(int i=1;i<50;++i) { vec.push_back(i); } std::cout << "Print the odd numbers\n"; std::for_each( vec.begin(), vec.end(), if_then(_1%2,std::cout << constant(" ") << _1)); std::cout << "\n\nIf the number is even, double it, otherwise print it\n"; std::for_each( vec.begin(), vec.end(), if_(_1%2==0)[_1*=2].else_[std::cout << constant(" ") << _1]); std::cout << "\nVerify the above\n"; constant_type<char>::type space(constant(' ')); std::for_each( vec.begin(), vec.end(), if_(_1%2==0)[std::cout << space << _1]); }