std::string str = "1234xyz"; size_t pos = str.find_last_not_of("0123456789"); if (pos != std::string::npos) { std::cout << "Last non-digit character is '" << str[pos] << "'" << std::endl; } else { std::cout << "No non-digit character found." << std::endl; }
std::string str = " hello world "; str.erase(str.find_last_not_of(" \t\r\n")+1);This code removes any whitespace characters (spaces, tabs, newlines) from the end of a string. The `find_last_not_of` function is used to find the last non-whitespace character in the string (in this case, the letter 'd'). The `+1` ensures that the erase function removes all characters from the last non-whitespace character onwards. The resulting string would be `" hello world"`. The `find_last_not_of` function is part of the `std` namespace, which is contained in the C++ Standard Library. No additional package or library is required to use it.