#include#include int main() { std::string str = " Hello, world! "; str.erase(0, str.find_first_not_of(" ")); std::cout << str << std::endl; return 0; }
#includeThis example demonstrates how to trim trailing spaces from the "str" string using "string::erase" and "string::find_last_not_of" functions. The latter method finds the last character in the string that is not a space and returns its position. Both examples above make use of features in the C++ STL (Standard Template Library). Specifically, the "string" class is part of the library, and it provides methods such as "find_first_not_of" and "erase" that facilitate string trimming.#include int main() { std::string str = " Hello, world! "; str.erase(str.find_last_not_of(" ") + 1); std::cout << str << std::endl; return 0; }