我正在学习 Accelerated C++ 这本书,并编写一个函数将字符串拆分为单词 vector (由空格字符分隔),使用了 find_if
。
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;
}
以及space
和not_space
的定义:
bool space(char c) {
return isspace(c);
}
bool not_space(char c) {
return !isspace(c);
}
是否有必要在这里写两个单独的谓词,或者可以简单地传递 !space
代替 not_space
?
最佳答案
只需使用 std::not1(std::ptr_fun(space))
. std::not1
在 <functional>
中声明.
(还有一个 std::not2
用于二元谓词;std::not1
用于一元谓词。)
关于c++ - C++ 中谓词的逻辑否定,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24589895/