I was working with some strings and I wondered how you can trim a string in C++. Having iterators and so many algorithms (too many?) in the Standard Library gives a lot of flexibility, and some tasks were left out of the standards.
The flexibility of C++ feels like morally pushing you to also write flexible code which can cover a lot of needs. Most probably some things could be improved to my implementation of string trimming.
The functions are ltrim (erase from left), rtrim (erase from right) and trim (erase from left and right). All three take a reference to a string (the input string is modified) and a predicate function to match the characters you want to erase (std::isspace as default):
using Predicate = std::function<int(int)>; static inline void ltrim(std::string &str, Predicate const &pred = isspace); static inline void rtrim(std::string &str, Predicate const &pred = isspace); static inline void trim(std::string &str, Predicate const &pred = isspace);