簡體   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