I want to delete all characters between two same characters in a string. My function takes a string (by reference) and a char in its arguments.
Assuming that I used an std::string variable like this: "hah haaah hah hello!" as the first parameter and a char 'h' as the second parameter, something like this should happen: "hah haaah hah hello!" ===> "hh hh hh hello". As you can see, every character between two h characters has been removed. How do I achive something like this?
I've tried to use iterators and ended up with this:
void delete_chars_between(std::string& line, char del)
{
    std::string::iterator itr_from = std::find(line.begin(), line.end(), del);
    std::string::iterator itr_to = std::find(itr_from + 1, line.end(), del);
    while (true) {
        if(itr_to != line.end())
            line.erase(itr_from + 1, itr_to);
        itr_from = std::find(itr_to, line.end(), del);
        if (itr_from == line.end())
            break;
        itr_to = std::find(itr_from + 1, line.end(), del);
        if (itr_to == line.end())
            break;
    }
}
First, I search for the first occurrence of del, I store the iterator to its position in itr_from. After that, I search for the second occurrence of del. And finally I run a while loop that starts by erasing characters in a certain range if itr_to is valid. I repeat that over and over again while my iterators are not equal to line.end().
But for some reason, this code doesn't work properly. It sometimes removes whitespaces and doesn't even touch the characters I was aiming to delete.
Thanks for your help.
 
     
     
    