纯C++风格,没有使用#include <boost/bind.hpp>
#include <iostream>
#include <algorithm>
#include <functional>
#include <vector> class add :public std::binary_function<int, int, void>
{
public:
void operator()(int i, int j)const
{
std::cout << i + j << std::endl;
}
}; void main()
{
std::vector<int>myv; myv.push_back();
myv.push_back();
myv.push_back(); for_each(myv.begin(), myv.end(), std::bind1st(add(), ));//实现每个元素+10并输出,但不改变原有的元素
}
使用#include <boost/bind.hpp>
#include <iostream>
#include <algorithm>
#include <functional>
#include <vector>
#include <boost/bind.hpp> void add(int i, int j)
{
std::cout << i + j << std::endl;
} void main()
{
std::vector<int>myv; myv.push_back();
myv.push_back();
myv.push_back(); for_each(myv.begin(), myv.end(), boost::bind(add, , _1));//实现每个元素+13并输出,但不改变原有的元素
}