-
Notifications
You must be signed in to change notification settings - Fork 0
/
functor.cpp
44 lines (36 loc) · 940 Bytes
/
functor.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// functor.cpp -- using a functor
#include <iostream>
#include <list>
#include <iterator>
#include <algorithm>
template<class T>
class TooBig{
private:
T cutoff;
public:
TooBig(const T & t):cutoff(t){}
bool operator()(const T & v){return v>cutoff;}
};
void outint(int n){std::cout<<n<<" ";}
int main(){
using std::list;
using std::cout;
using std::endl;
TooBig<int> f100(100);
int vals[10]={50, 100, 90, 180, 60, 210, 415, 88, 188, 201};
list<int> yadayada(vals, vals+10);
list<int> etcetera(vals, vals+10);
cout<<"Original lists:\n";
for_each(yadayada.begin(),yadayada.end(), outint);
cout<<endl;
for_each(etcetera.begin(),etcetera.end(),outint);
cout<<endl;
yadayada.remove_if(f100);
etcetera.remove_if(TooBig<int>(200));
cout<<"Trimmed lists:\n";
for_each(yadayada.begin(),yadayada.end(),outint);
cout<<endl;
for_each(etcetera.begin(),etcetera.end(),outint);
cout<<endl;
return 0;
}