可以通过把其它字符替换为空格,利用输入符自动去掉空格来完成,但是这道题有个坑,看代码

#include <string>
#include <map>
#include <vector>
#include <sstream>

using namespace std;

int main() {
    string str;
    getline(cin ,str) ;//只能用getline,不能while(cin>>str),不信你试试
        for (int i = 0; i < str.size(); i++)
        {
            if (!isalpha(str[i])) str[i] = ' ';
        }
        stringstream input(str);//这里只能有一个流如果有其他的流,它遇到空白会跳出,但只有一个流他就会一直输入
        //str.clear();
        string word,temp;
        while (input >> word) {
            if (temp.empty())temp = word;
            else temp = word + " " + temp;

        }
        cout << temp;
    }