I found the following code in the book “Accelerated C++” (Chapter 6.1.1), but I can’t compile it. The problem is with the find_if lines. I have the necessary includes (vector, string, algorithm, cctype). Any idea?
Thanks, Jabba
bool space(char c) {
return isspace(c);
}
bool not_space(char c) {
return !isspace(c);
}
vector<string> split_v3(const string& str)
{
typedef string::const_iterator iter;
vector<string> ret;
iter i, j;
i = str.begin();
while (i != str.end())
{
// ignore leading blanks
i = find_if(i, str.end(), not_space);
// find end of next word
j = find_if(i, str.end(), space);
// copy the characters in [i, j)
if (i != str.end()) {
ret.push_back(string(i, j));
}
i = j;
}
return ret;
}
There is no problem in the code you posted. There is a very obvious problem with the real code you linked to:
is_spaceandspaceare member functions, and they cannot be called without an instance of Split2. This requirement doesn’t make sense, though, so at least you should make those functions static.(Actually it doesn’t make much sense for split_v3 to be a member function either. What does having a class called Split2 achieve over having just a free function – possibly in a namespace?)