题解 | #句子逆序#
句子逆序
http://www.nowcoder.com/practice/48b3cb4e3c694d9da5526e6255bb73c3
#include <iostream>
#include <vector>
#include <algorithm> // reverse
#include <sstream> // 为了拆解单词
#include <string> // 为了getline带空格的字符串
using namespace std;
int main(){
string str;
getline(cin, str);
istringstream ss(str);
string s;
vector<string> temp;
while(ss >> s){
temp.push_back(s);
}
for(int i=0; i<temp.size(); i++){
cout << temp[temp.size()-i-1]<<" ";
}
return 0;
} 本题的关键点在于利用<sstream>
然后通过istringstream ss(str), 将str分解成一个个的单词
然后通过vector进行保存后倒序输出</sstream>
查看3道真题和解析