I'm following the book Accelerated C++, and to write a function to split a string into a vector of words (separated by space characters), find_if
is utilized.
vector<string> split(const string& str) {
typedef string::const_iterator iter;
vector<string> ret;
iter i = str.begin();
while (i != str.end()) {
i = find_if(i, str.end(), not_space);
iter j = find_if(i, str.end(), space);
if (i != str.end())
ret.push_back(string(i, j));
i = j;
}
return ret;
}
and the definitions of space
and not_space
:
bool space(char c) {
return isspace(c);
}
bool not_space(char c) {
return !isspace(c);
}
Is it necessary to write two separate predicates here, or could one simply pass !space
in place of not_space
?