gpt4 book ai didi

c++ - C++ 中谓词的逻辑否定

转载 作者:行者123 更新时间:2023-11-28 00:26:59 25 4
gpt4 key购买 nike

我正在学习 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;
}

以及spacenot_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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com