1 #include <iostream> 2 #include <string> 3 #include <vector> 4 #include <algorithm> 5 #include <functional> 6 using namespace std; 7 8 9 //绑定函数的默认值,继承二进制函数类的所有类容 10 class add :public std::binary_function<int, int, void> 11 { 12 13 public: 14 void operator()(int i, int j) const 15 { 16 cout << i <<" "; 17 cout << j << " "; 18 std::cout << i + j << endl; 19 } 20 21 22 }; 23 24 //void add(int i, int j) 25 //{ 26 // std::cout << i + j << endl; 27 // 28 //} 29 30 void main() 31 { 32 vector<int> myv; 33 myv.push_back(11); 34 myv.push_back(23); 35 myv.push_back(34); 36 37 //仿函数绑定第一个参数 38 for_each(myv.begin(), myv.end(), bind1st(add(),10)); 39 //普通函数绑定一个参数 40 //for_each(myv.begin(), myv.end(), bind(add, 13, _1)); 41 42 cin.get(); 43 }