0

I wanted to delete one symbol ':' from the string.

std::cout << str << " "; //1948: 59 63
std::remove(str.begin(), str.end(), ':');
std::cout << str << " ";//1948 59 633

In case when I change the range to [str.negin(), str.begin()+5) everything is ok.

Konstantin
  • 111
  • 1
  • 8

2 Answers2

3

std::remove does not remove elements from a range. Removing elements using iterators is not possible. However, it moves all the elements that you don't want to remove to the beginning, and then returns an iterator pointing to the end of those items.

The correct way to use std::remove is like this:

str.erase(std::remove(str.begin(), str.end(), ':'), str.end());

It will move all the "good" elements to the front (std::remove does that) and then it will erase the remaining ones (str.erase does that).

user253751
  • 57,427
  • 7
  • 48
  • 90
1

You have to erase the redundant symbol (or symbols).

For example

str.erase( std::remove(str.begin(), str.end(), ':'), str.enc() );

The algorithm std::remove moves specified elements that should be removed) to the end of the used container. More precisely it moves actual elements to the beginning of the container taking the places of the removed elements.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335