I wanted to write two one-line functions which will trim white spaces from left and right side of a string. Left side weren’t problem:
void trimLeft(string &s) {
s.erase(s.begin(), find_if(s.begin(), s.end(), (int (*)(int))isgraph));
}
But when I tried something similar for right side:
void trimRight(string &s) {
s.erase(find_if(s.rbegin(), s.rend(), (int (*)(int))isgraph), s.end());
}
I had some compiler errors. The problem is that I must convert reverse_iterator (which is returned by find_if) to normal iterator. How to do this?
You can use the
base()member function to recover the underlying iterator from its reverse_iterator.