| 1 |
#include <algorithm> |
| 2 |
#include <iterator> |
| 3 |
#include <iostream> |
| 4 |
#include <cstring> |
| 5 |
#include "predicates.h" |
| 6 |
using namespace std; |
| 7 |
|
| 8 |
//CONTAINS |
| 9 |
class Contains |
| 10 |
{ |
| 11 |
string d_target; |
| 12 |
size_t d_count; |
| 13 |
public: |
| 14 |
Contains(char const *target) |
| 15 |
: |
| 16 |
d_target(target), |
| 17 |
d_count(0) |
| 18 |
{} |
| 19 |
bool operator()(string const &str) |
| 20 |
{ |
| 21 |
d_count++; |
| 22 |
return str.find_first_of(d_target) != string::npos; |
| 23 |
} |
| 24 |
bool operator()(string const &s1, string const &s2) |
| 25 |
{ |
| 26 |
d_count++; |
| 27 |
return s1.find_first_of(s2) != string::npos; |
| 28 |
} |
| 29 |
size_t count() const |
| 30 |
{ |
| 31 |
return d_count; |
| 32 |
} |
| 33 |
}; |
| 34 |
//= |
| 35 |
//MAIN |
| 36 |
int main(int argc, char **argv) |
| 37 |
{ |
| 38 |
Contains contains{ "aeiou" }; |
| 39 |
if (argc == 1) |
| 40 |
cout << "Counted " << |
| 41 |
count_if(istream_iterator<string>{ cin }, |
| 42 |
istream_iterator<string>{}, |
| 43 |
PredicateObject1<Contains, string>{contains} |
| 44 |
) << " words containing vowels "; |
| 45 |
else |
| 46 |
cout << "Counted " << |
| 47 |
count_if(istream_iterator<string>{ cin }, |
| 48 |
istream_iterator<string>{}, |
| 49 |
[&](string const &target) |
| 50 |
{ |
| 51 |
return PredicateObject2<Contains, string> |
| 52 |
{ contains }(target, "aeiou"); |
| 53 |
} |
| 54 |
) << " words containing vowels "; |
| 55 |
cout << "(read " << contains.count() << " strings)\n"; |
| 56 |
} |
| 57 |
/* |
| 58 |
Generated output after executing (predobj.cc being this soure file) |
| 59 |
|
| 60 |
a.out < predobj.cc |
| 61 |
or |
| 62 |
a.out 2 < predobj.cc |
| 63 |
|
| 64 |
Counted 107 words containing vowels (read 163 strings) |
| 65 |
*/ |
| 66 |
//= |