<pre name="code" class="cpp">// boost.lambda表达式用法 // made by davidsu33 // 2014-9-22 #include "stdafx.h" #include <boost/typeof/typeof.hpp> #include <boost/lambda/lambda.hpp> #include <boost/bind/bind.hpp> #include <boost/assign.hpp> #include <boost/core/enable_if.hpp> #include <boost/operators.hpp> #include <vector> #include <utility> #include <memory> #include <algorithm> #include <cassert> #include <iostream> using namespace std; //Boost的简单用法 void lambda_easy_use() { using namespace boost::assign; vector<int> arr = list_of(0).repeat(19, 0); int begin = 0; for_each(arr.begin(), arr.end(), ( boost::lambda::_1 = boost::lambda::var(begin)++, boost::lambda::_1 += 10, boost::lambda::_1 -= 5 ) ); for (unsigned i=0; i<arr.size(); ++i) { assert(arr[i] == 5+i); } cout<<"lambda_easy_use passed"<<endl; } //Boost的仿函数调用 void lambda_functors() { int i=10, j=20; int r = (boost::lambda::_1 + boost::lambda::_2)(i,j); assert(r == (i+j)); BOOST_AUTO(br, (boost::lambda::_1 - boost::lambda::_2)(i, j)); assert(br == (i-j)); //多次运算 BOOST_AUTO(br2, (boost::lambda::_1 - boost::lambda::_2, boost::lambda::_1 += boost::lambda::constant(10))(i, j)); //??br2 == 20,br2的取值由第二个表达式赋予 assert(br2 == i); assert(i == 20); } template<class T> T fun(const T& value) { //要求输入值必须为整型 typedef boost::enable_if<boost::is_integral<T>, T>::type TType; cout<<"fun is called!"<<endl; cout<<"the value:"<<value<<endl; return value+100; } //通过boost operator实现>操作符 struct SortedData :boost::less_than_comparable1<SortedData> { SortedData():x(0),y(0){}; SortedData(double sx, double sy):x(sx),y(sy){}; bool operator<(const SortedData& other) const { return dist() < other.dist(); } double dist() const { return sqrt(x*x + y*y); } void fillRandamData() { x = rand() %1000; y = rand() % 500; } friend ostream& operator<<(ostream & ostr, const SortedData& s) { ostr << "x=" << s.x <<" y="<<s.y<<endl; return ostr; } //x dist double x; //y dist double y; }; void lambda_call() { vector<int> v; using namespace boost::assign; v += 1,2,4,8,16; for_each(v.begin(), v.end(), fun<int>); //准备排序数据 SortedData s1(10, 20), s2(1,2); assert(s1 > s2); //boost::lambda::placehoder1_type 等价于boost::lambda::_1 //详见boost::lambda::core.hpp文件 vector<SortedData> vs2(10); std::for_each(vs2.begin(), vs2.end(), boost::bind(&SortedData::fillRandamData, boost::lambda::placeholder1_type())); std::copy(vs2.begin(), vs2.end(), ostream_iterator<SortedData>(cout, "\n")); //进行排序 cout<<"排序结果"<<endl; std::sort(vs2.begin(), vs2.end(), boost::lambda::_1 < boost::lambda::_2); std::copy(vs2.begin(), vs2.end(), ostream_iterator<SortedData>(cout, "\n")); } void lambda_access_member() { vector<SortedData> vs2(10); std::for_each(vs2.begin(), vs2.end(), boost::bind(&SortedData::fillRandamData, boost::lambda::placeholder1_type())); std::copy(vs2.begin(), vs2.end(), ostream_iterator<SortedData>(cout, "\n")); //进行排序 cout<<"排序结果"<<endl; std::sort(vs2.begin(), vs2.end(), boost::lambda::_1 < boost::lambda::_2); std::copy(vs2.begin(), vs2.end(), ostream_iterator<SortedData>(cout, "\n")); SortedData s; boost::bind<double&>(&SortedData::x, boost::lambda::_1)(s) = 100; assert(s.x == 100); double m=0; //std::for_each(vs2.begin(), vs2.end(), boost::bind<double>(&SortedData::x, boost::lambda::_1) ); //std::for_each(vs2.begin(), vs2.end(), boost::bind(boost::type<double>(),&SortedData::y, boost::lambda::_1) = 200); } int _tmain(int argc, _TCHAR* argv[]) { lambda_easy_use(); lambda_functors(); lambda_call(); lambda_access_member(); getchar(); return 0; }