1

此代码有效,但有点受限,所以如果它不等于字母,我想删除一些东西。

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

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

任何帮助表示赞赏。

4

1 回答 1

6

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

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());
于 2012-03-19T19:47:40.437 回答