str.erase(0,str.find_first_not_of("\r\t\n ")); str.erase(str.find_last_not_of("\r\t\n ")+1); 使用以上两行代码即可简单实现trim()的功能,去除首尾空格和回车。