size_t word_count(const string& doc) { string::const_iterator cur = doc.begin(); string::const_iterator end = doc.end(); size_t res = 0; while (cur != end) { cur = find_if(cur, end, isalpha); if (cur == end) break; ++res; cur = find_if(cur, end, not1(isalpha)); } return res;}
C++ (Qt)template <class String, class FindWord>size_t word_count(const String& doc, FindWord find_word) { typedef String::const_iterator Iter; Iter cur = doc.begin(); Iter end = doc.end(); size_t res = 0; while (cur != end) { pair<iter, iter> word = find_word(cur, end); if (word.first == end) break; ++res; cur = word.second; } return res;}
C++ (Qt)template <class Iter>struct IsAlphaWord { pair<Iter, Iter> operator() (Iter cur, Iter end) { Iter start = find_if(cur, end, isalpha); if (start == end) return make_pair(end, end); return make_pair(start, find_if(start, end, not1(isalpha))); }};