// 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 // // This code removes every other vowel from the string. // The functor maintains a data member “count” to keep track // of which call we're doing. #include #include #include #include #include using namespace std; class IsVowel { int count; public: IsVowel() : count(0) {} bool operator()(char c) { return string("aeiouyAEIOUY").find(c) != string::npos && (++count % 2) == 0; } }; int main() { char name[] = "James Tiberius Kirk"; remove_copy_if(name, name+strlen(name), ostream_iterator(cout), IsVowel()); cout << '\n'; }