简体   繁体   English

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

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

This code works but it is a bit limited so I want to remove something if it is not equal to a letter. 该代码有效,但有一点局限性,因此如果它不等于字母,我想删除一些内容。

I know that I have to use ::isalpha instead of ::ispunct but I don't understand how to make it remove if it is not equal to :: isalpha. 我知道我必须使用:: isalpha而不是:: ispunct,但是如果它不等于:: isalpha,我不知道如何删除它。 I have goggled this question but didn't get anywhere with the answers because I didn't understand them. 我已经对这个问题进行了调查,但是没有得到答案,因为我听不懂。

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

Any Help is appreciated. 任何帮助表示赞赏。

I haven't compiled, but this should work: 我还没有编译,但这应该可以工作:

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

The links of interest here are: 这里感兴趣的链接是:

If standard functors didn't suffice, you could also implement your own: 如果标准函子不足,您也可以实现自己的函数:

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

Which can be used as: 可以用作:

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