// This file, part of a series, show why functors are so cool, // because they can maintain state information: // - from the constructor // - from call-to-call // // Instead of hard-coding “2”, so that the code removes every 2nd vowel, // the functor has a data member “period”, initialized in the ctor. #include #include #include #include #include using namespace std; class IsVowel { const int period; // Hey, const! int count; public: IsVowel(int how_often) : period(how_often), count(0) {} bool operator()(char c) { return string("aeiouyAEIOUY").find(c) != string::npos && (++count % period) == 0; } }; int main() { char name[] = "James Tiberius Kirk"; remove_copy_if(name, name+strlen(name), ostream_iterator(cout), IsVowel(2)); cout << '\n'; }