C++ 从使用sstream读取的单词中删除最后一个空格的最佳方法?

C++ 从使用sstream读取的单词中删除最后一个空格的最佳方法?,c++,stringstream,C++,Stringstream,所以,我的代码应该是在不改变输入顺序的情况下对句子中的单词进行置乱。代码工作正常,但在输出的末尾有一个空格,这会导致显示错误 #include <iostream> #include <string> #include <sstream> using namespace std; int main() { string line; while(getline(cin,line)){ stringstream ss(line)

所以,我的代码应该是在不改变输入顺序的情况下对句子中的单词进行置乱。代码工作正常,但在输出的末尾有一个空格,这会导致显示错误

#include <iostream>
#include <string>
#include <sstream>

using namespace std;

int main() {
    string line;
    while(getline(cin,line)){
        stringstream ss(line);
        string word;
        while(ss>>word){
            string sWord;
            for(int i=word.length()-1;i>=0;i--) {
                sWord+=word.at(i);
            } 
            cout << sWord << " ";
        } 
    cout << endl;
    }
}
#包括
#包括
#包括
使用名称空间std;
int main(){
弦线;
while(getline(cin,line)){
弦流ss(线);
字符串字;
while(ss>>word){
弦剑;
对于(int i=word.length()-1;i>=0;i--){
剑+=单词at(i);
} 

cout您可以使用
bool
测试是否显示第一个单词,如:

bool is_first = true; // bool flag to test whether first word or not 
while(ss>>word){
        string sWord;
        for(int i=word.length()-1;i>=0;i--) {
            sWord+=word.at(i);
        }
        if(is_first){ // first word
            cout << sWord;
            is_first = false;
        }
        else{ // not first word
            cout << " " << sWord;
        }
} 
bool is_first=true;//测试是否为第一个单词的bool标志
while(ss>>word){
弦剑;
对于(int i=word.length()-1;i>=0;i--){
剑+=单词at(i);
}
if(is_first){//第一个单词

我可以按照这句话做更多的宣传:

while(getline(cin,line))
{
    stringstream ss(line);
    string word;
    if (ss>>word)
    {
        string sWord;
        for(int i=word.length()-1;i>=0;i--) 
        {
            sWord=word.at(i);
        } 
        cout << sWord;
        while(ss>>word)
        {
            for(int i=word.length()-1;i>=0;i--) 
            {
                sWord+=word.at(i);
            } 
            cout << " " << sWord;
        } 
    }
    cout << endl;
}
while(getline(cin,line))
{
弦流ss(线);
字符串字;
如果(ss>>word)
{
弦剑;
对于(int i=word.length()-1;i>=0;i--)
{
剑=单词。at(i);
} 
cout>word)
{
对于(int i=word.length()-1;i>=0;i--)
{
剑+=单词at(i);
} 

是否可以考虑在实际单词前面加空格:

int main () {
    string line;
    while(getline (cin, line)) {
        stringstream ss (line);
        string word;
        bool first = true;
        while(ss >> word) {
            if(first) {
                first = false; //skip the space for the first word
            } else cout << " ";
            string sWord;
            for(int i = word.length () - 1; i >= 0; i--) {
                sWord += word.at (i);
            }
            cout << sWord;
        }
        cout << endl;
    }
}
int main(){
弦线;
while(getline(cin,line)){
弦流ss(线);
字符串字;
bool first=true;
while(ss>>word){
如果(第一){
first=false;//跳过第一个单词的空格
}else cout=0;i--){
剑+=单词at(i);
}

cout-Hey,谢谢你的提示!我写的有点不同,但它做到了。我没有使用
else
,而是使用
is_first=false;continue;
。从来没有想过使用布尔变量。我想我缺乏经验。@Gabriellebello没错,continue同样好。生成的代码可能完全相同。@Gabrieller埃贝罗相关:
int main () {
    string line;
    while(getline (cin, line)) {
        stringstream ss (line);
        string word;
        bool first = true;
        while(ss >> word) {
            if(first) {
                first = false; //skip the space for the first word
            } else cout << " ";
            string sWord;
            for(int i = word.length () - 1; i >= 0; i--) {
                sWord += word.at (i);
            }
            cout << sWord;
        }
        cout << endl;
    }
}