29

 1 #include <iostream>
 2 #include <string>
 3 using namespace std;
 4 template<class T,class Pred>  
 5 T* Filter(T* p,T* q,T* s,Pred op){
 6     while(p < q){
 7         if(op(*p)){
 8             *s = *p;
 9             ++ s;
10         }
11         ++ p;
12     }
13     return s;
14 }
15 bool LargerThan2(int n)
16 {
17     return n > 2;
18 }
19 bool LongerThan3(string s) 
20 {
21     return s.length() > 3;
22 }
23 
24 string as1[5] = {"Tom","Mike","Jack","Ted","Lucy"};
25 string as2[5];
26 int  a1[5] = { 1,2,3,4,5};
27 int a2[5];
28 int main() {
29     string * p = Filter(as1,as1+5,as2,LongerThan3);
30     for(int i = 0;i < p - as2; ++i)
31         cout << as2[i];
32     cout << endl; 
33     int * p2 = Filter(a1,a1+5,a2,LargerThan2);
34     for(int i = 0;i < p2-a2; ++i)
35         cout << a2[i] << ",";
36     return 0;
37 }

 

posted @ 2022-09-13 22:52  balabalahhh  阅读(50)  评论(0编辑  收藏  举报