我不明白如何在c ++中执行remove_if

bob*_*mac 1 c++ erase remove-if

这段代码有效,但有点限制,所以如果它不等于一个字母,我想删除一些东西.

我知道我必须使用:: isalpha而不是:: ispunct但我不明白如果它不等于:: isalpha,如何删除它.我已经解决了这个问题,但没有得到答案,因为我不理解它们.

textFile[i].erase(remove_if(textFile[i].begin(), textFile[i].end(), ::ispunct), textFile[i].end());
Run Code Online (Sandbox Code Playgroud)

任何帮助表示赞赏.

Bil*_*nch 6

我没有编译,但这应该工作:

textFile[i].erase(
    remove_if(textFile[i].begin(), textFile[i].end(), std::not1(std::ptr_fun(::isalpha))),
    textFile[i].end());
Run Code Online (Sandbox Code Playgroud)

这里感兴趣的链接是:

如果标准仿函数不够用,您也可以实现自己的:

struct not_a_character : std::unary_function<char, bool> {
    bool operator()(char c) const {
        return !isalpha(c);
    }
};
Run Code Online (Sandbox Code Playgroud)

哪个可以用作:

textFile[i].erase(
    remove_if(textFile[i].begin(), textFile[i].end(), not_a_character()),
    textFile[i].end());
Run Code Online (Sandbox Code Playgroud)