#include <iostream> #include <vector> #include <sstream> #include <algorithm> using namespace std; int main() { string str; getline(cin, str); // 分割 单词 vector<string> words; // 可以将字符串按空格分割成多个单词 stringstream ss(str); string word; while (ss >> word) { // 每次取一个单词 words.push_back(word); } // 翻转数组; reverse(words.begin(), words.end()); // 方法一:重新组合句子 /*stringstream result; for (int i = 0; i < words.size(); i++) { if (i > 0) result << " "; result << words[i]; } cout << result.str() << endl; */ // 方法二: for (auto w : words) { cout << w << " "; } return 0; }