【C++】去除字符串string中的空格(两头空格、所有空格)

去除首尾空格:

std::string& trim(std::string &s) 
{
    if (!s.empty()) 
    {
        s.erase(0,s.find_first_not_of(" "));
    	s.erase(s.find_last_not_of(" ") + 1);
    }
    return s;
}

去除所有空格:

void trim(string &s)
{
	int index = 0;
	if(!s.empty())
	{
		while( (index = s.find(' ',index)) != string::npos)
		{
			s.erase(index,1);
		}
	}
}
原文地址:https://www.cnblogs.com/dindin1995/p/13059075.html