So after trying to solve a problem for several hours i am very close. I need to remove all characters that is not alphabetical but currently i am removing all alphabetical characters and keeping everything else.
This is the piece of code that currently removes all alphabetical characters instead of removing all other characters except the alphabetical. Please help me make this a "remove_if_not" function. I have no idea where to start after experimenting a lot.
NotAlphaNum should have the same return type and parameter has isalnum.
When passing to remove_if, you're passing the function itself, so you don't need the brackets.
Hmm i am not really sure what that parameter should be. Maybe i need to take a break from this and do i again in a few hours. My brain has stopped working. :D
You say don't what type of container tempbook is, but based on your other posts, I'm assuming it is a string. It doesn't really matter a whole lot as long as the container supports a forward iterator.
You're trying to combine two functions in a way that won't work.
- string.erase() removes a character pointed to by an iterator.
- remove_if() removes those elements that match the predicate.
The code you have is going to result in undefined behavior. tempbook.erase() expects an iterator pointing to the element to be removed from the container. remove_if() returns a new "past end" iterator as its return value, which is what is passed to tempbook.erase() and that is what tempbook.erase is going to try and remove. So tempbook.erase() is going to try and remove an element past the end of the container (string).
To answer your question, simply supply your own predicate to remove_if that returns true if the character is not alphanumeric.
You're trying to combine two functions in a way that won't work.
- string.erase() removes a character pointed to by an iterator.
- remove_if() removes those elements that match the predicate.
One version of string.erase() removes a character pointed to by an iterator. Not the version used here. And, perhaps somewhat surprisingly, remove_if doesn't remove anything.
The code you have is going to result in undefined behavior.
Nope!
remove_if() returns a new "past end" iterator as its return value,
What remove_if returns is a valid iterator that, if remove_if actually removed anything, would be the iterator returned by tempbook.end(). But, since it doesn't actually remove anything, we need to erase those elements from that iterator to the end of the string in order to finish the job.