40 lines
886 B
C++
40 lines
886 B
C++
|
// 算术类函数对象
|
||
|
// 算术类函数对象
|
||
|
#include <iostream>
|
||
|
#include <set>
|
||
|
#include <algorithm>
|
||
|
|
||
|
using namespace std;
|
||
|
|
||
|
// template <typename T>
|
||
|
void print(typename set<int>::const_iterator start,
|
||
|
typename set<int>::const_iterator end, plus<int> pl, int m)
|
||
|
{
|
||
|
for (; start != end; start++)
|
||
|
{
|
||
|
cout << pl(*start, m) << " ";
|
||
|
}
|
||
|
cout << endl;
|
||
|
}
|
||
|
|
||
|
void print(typename set<int>::const_iterator start,
|
||
|
typename set<int>::const_iterator end, multiplies<int> mul, int m)
|
||
|
{
|
||
|
for (; start != end; start++)
|
||
|
{
|
||
|
cout << mul(*start, m) << " ";
|
||
|
}
|
||
|
cout << endl;
|
||
|
}
|
||
|
|
||
|
int main()
|
||
|
{
|
||
|
int m[] = {1, 2, 3, 4, 8, 5, 1, 2};
|
||
|
// set 会自动过滤重复,并排序
|
||
|
set<int> s(m, m + sizeof(m) / sizeof(m[0]));
|
||
|
print(s.begin(), s.end(), plus<int>(), 1);
|
||
|
print(s.begin(), s.end(), multiplies<int>(), 3);
|
||
|
|
||
|
return 0;
|
||
|
}
|