繁体   English   中英

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

[英]I don't understand how to do a remove_if not in c++

该代码有效,但有一点局限性,因此如果它不等于字母,我想删除一些内容。

我知道我必须使用:: isalpha而不是:: ispunct,但是如果它不等于:: isalpha,我不知道如何删除它。 我已经对这个问题进行了调查,但是没有得到答案,因为我听不懂。

textFile[i].erase(remove_if(textFile[i].begin(), textFile[i].end(), ::ispunct), textFile[i].end());

任何帮助表示赞赏。

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

textFile[i].erase(
    remove_if(textFile[i].begin(), textFile[i].end(), std::not1(std::ptr_fun(::isalpha))),
    textFile[i].end());

这里感兴趣的链接是:

如果标准函子不足,您也可以实现自己的函数:

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

可以用作:

textFile[i].erase(
    remove_if(textFile[i].begin(), textFile[i].end(), not_a_character()),
    textFile[i].end());

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM