I have the following code that takes a string and erases non alphabet characters
void removeNonAlpha(string& str){
for (string::iterator it = str.begin(); it < str.end(); it++){
if (!(isUpperCaseLetter(*it) || isLowerCaseLetter(*it) || str == ' '))
str.erase(it--);
}
}
I showed this to my professor and he told me that doing this is risky because it may invalidate the iterator that I'm using. However, I thought that erase will only invalidate iterators after the point of the erase, and I made sure not to use any iterators after that point. So could this code crash or cause any undefined behavior?
It won't compile.
str
is std::string andstr == ' '
are two different types.Secondly, when iterating reversed you need to use the string::reverse_iterator, string::rbegin() and string::rend();
std::vector::erase
works as you suggest; it only invalidates iterators starting with the first erased element. However, that doesn't apply tostd::string
.C++ allows string iterators to be invalidated at the drop of a hat.
The C++ standard has traditionally been more flexible with the requirements for
std::string
. (Or, in other words, it has traditionally allowed implementers to use optimizations which would not be valid for vectors.) And so it is withstd::string::erase
, and other string mutators.In
[string.require]
(§21.4.1 of n3797), the standard accepts that:basic_string
sequence may be invalidated by the following uses of thatbasic_string
object:basic_string
as an argument.operator[]
,at
,front
,back
,begin
,rbegin
,end
, andrend
.In other words, calling a potentially mutating function like
std::string::erase
could invalidate all iterators to that string, even if no visible modifications are made to the string (for example, because the range to be erased is empty).(The latest draft C++ standard has the same wording, although it is now paragraph 4.)
The proposed code involves undefined behaviour if the first character of the string is not alphabetic.
In the first loop through the string, the iterator
it
has the valuestr.begin()
. That iterator cannot be decremented, since the result would not be inside the string. And therefore, incrementing the decremented iterator might not returnit
tostr.begin()
for the next iteration.Use indices rather than iterators
None of the above applies to integer position indices. So if you could safely replace your loop with the very similar: