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

标签 c++

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

相关文章:

c++ - Gotw 67 中的一个例子

c++ - Tcl:解释器创建被跟踪对象的拷贝,当它发生变化时

C++程序错误

c++ - MSVC10 Visual Studio 2010是否支持C++显式转换运算符

C++ 在可变参数宏中使用多个参数(实现可选参数)

c++ - 并行将 64 位整数中的压缩 8 位整数减去 1,SWAR 无需硬件 SIMD

c++ - cvRound() 中的 x64 舍入不一致 (_mm_cvtsd_si32)

c++ - 查找可以从 CMD 运行的可执行文件的绝对路径

c++ - 在 iOS 中使用 C++ fuzzylite 库和 ObjC(模糊逻辑)

android - cpp文件的armeabi-v7a编译错误