void trim(string &str)
{
string::iterator it = str.begin();
string::iterator end = str.end() - 1;
// trim at the starting
for(; it != str.end() && isspace(*it); it++)
;
str.replace(str.begin(), it, "");
// trim at the end
for(; end >= str.begin() && isspace(*end); end--)
;
str.replace(str.end(), end, ""); // i get the out_of_range exception here
}
I want to trim a string of spaces. First I trip spaces from the starting and it works fine and then I find the positions of spaces from the end and try to remove it and it throws exception.
Why?