#include #include #include using namespace std; // template void print(typename set::const_iterator start, typename set::const_iterator end, plus pl, int m) { for (; start != end; start++) { cout << pl(*start, m) << " "; } cout << endl; } void print(typename set::const_iterator start, typename set::const_iterator end, multiplies mul, int m) { for (; start != end; start++) { cout << mul(*start, m) << " "; } cout << endl; } void show(int n) { cout << n << " "; } // 二元仿函数 class PrintPlus : public binary_function { public: void operator()(const int &n1, const int &n2) const { cout << n1 << "+" << n2 << " = " << n1 + n2 << endl; } }; int main() { int m[] = {1, 2, 3, 4, 8, 5, 1, 2}; // set 会自动过滤重复,并排序 set s(m, m + sizeof(m) / sizeof(m[0])); // print(s.begin(), s.end(), plus(), 1); // print(s.begin(), s.end(), multiplies(), 3); // for_each(s.begin(), s.end(), show); // cout << endl; // PrintPlus 测试 // for (int i = 0; i < 6; i++) for_each(s.begin(), s.end(), bind1st(PrintPlus(), i)); return 0; }