文字列をトリミングする簡単なコードがあります
std::string TrimEnd(const std::string& str, const std::string& chars)
{
std::string trimmed = str;
int index = 0;
if((index = trimmed.find_last_not_of(chars)) < trimmed.length() - 1)
trimmed.erase(index + 1);
return trimmed;
}
引数には参照を使用し、関数の戻り値には値を使用します。代わりにポインタを使用する設定はありますか?私の場合、トリミングされた文字列のコピーが返されます。一部の関数では、const参照が返されます。