题解 | #字符串排序# stable_sort
字符串排序
https://www.nowcoder.com/practice/5190a1db6f4f4ddb92fd9c365c944584
#include <algorithm>
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
bool cmp(char c1, char c2) {
return tolower(c1) < tolower(c2);
}
int main() {
string str_input, str_output;
getline(cin, str_input);
for (auto &c : str_input) {
if (isalpha(c)) str_output += c;
}
stable_sort(str_output.begin(), str_output.end(), cmp);
for (int i = 0; i < str_input.length(); ++i) {
if (isalpha(str_input[i])) continue;
str_output.insert(str_output.begin() + i, str_input[i]);
}
cout << str_output << endl;
}
// 64 位输出请用 printf("%lld")
查看12道真题和解析