46 lines
871 B
C++
46 lines
871 B
C++
#include <iostream>
|
|
#include <string>
|
|
#include <vector>
|
|
#include <algorithm>
|
|
#include "split.h"
|
|
|
|
using std::cin;
|
|
using std::cout;
|
|
using std::endl;
|
|
using std::string;
|
|
using std::vector;
|
|
|
|
// 函数:反转字符串
|
|
string reverseString(const string& s) {
|
|
return string(s.rbegin(), s.rend());
|
|
}
|
|
|
|
int main() {
|
|
// 读取输入文本
|
|
cout << "输入一段文本: ";
|
|
string input;
|
|
std::getline(cin, input);
|
|
|
|
// 拆分文本为单词
|
|
vector<string> words = split(input);
|
|
|
|
// 对每个单词进行反转
|
|
for (auto& word : words) {
|
|
word = reverseString(word);
|
|
}
|
|
|
|
// 按照反转后的单词排序
|
|
std::sort(words.begin(), words.end());
|
|
|
|
// 输出排序后的单词,保持与原输入格式一致
|
|
for (size_t i = 0; i < words.size(); ++i) {
|
|
if (i > 0) {
|
|
cout << " "; // 单词间加空格
|
|
}
|
|
cout << words[i];
|
|
}
|
|
cout << endl;
|
|
|
|
return 0;
|
|
}
|