Question

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. 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.

Was it helpful?

Solution

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());
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top