酷代码 AI
菜单
服务商

C++实现:反转字符串中单词顺序,消除多余空格

```cpp #include <iostream> #include <cstdlib> #include <string> #include <sstream> #include <vector> using namespace std; string reverseWords(string s) { vector<string> words; stringstream ss(s); string word; // 从输入流中提取单词 while (ss >> word) { words.push_back(word); } string result; // 从后往前遍历单词向量 for (int i = words.size() - 1; i >= 0; --i) { result += words[i]; if (i != 0) { result += " "; } } return result; } ``` ######[AI问答 | 167点数解答 | 2025-11-12 14:16:28]

相关提问
本站限时免费提问中
实用工具查看更多
Linux在线手册 [开发类]
Jquery在线手册 [开发类]
今日油价 [生活类]
图片互转base64 [开发类]
时间转换器 [开发类]