标签:
纯C++风格,没有使用#include <boost/bind.hpp>
1 #include <iostream> 2 #include <algorithm> 3 #include <functional> 4 #include <vector> 5 6 class add :public std::binary_function<int, int, void> 7 { 8 public: 9 void operator()(int i, int j)const 10 { 11 std::cout << i + j << std::endl; 12 } 13 }; 14 15 void main() 16 { 17 std::vector<int>myv; 18 19 myv.push_back(11); 20 myv.push_back(23); 21 myv.push_back(34); 22 23 for_each(myv.begin(), myv.end(), std::bind1st(add(), 10));//实现每个元素+10并输出,但不改变原有的元素 24 }
使用#include <boost/bind.hpp>
1 #include <iostream> 2 #include <algorithm> 3 #include <functional> 4 #include <vector> 5 #include <boost/bind.hpp> 6 7 void add(int i, int j) 8 { 9 std::cout << i + j << std::endl; 10 } 11 12 void main() 13 { 14 std::vector<int>myv; 15 16 myv.push_back(11); 17 myv.push_back(23); 18 myv.push_back(34); 19 20 for_each(myv.begin(), myv.end(), boost::bind(add, 13, _1));//实现每个元素+13并输出,但不改变原有的元素 21 }
标签:
原文地址:http://www.cnblogs.com/denggelin/p/5767975.html