题解 | #字符串排序# 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")

全部评论

相关推荐

头像
04-09 14:29
Java
点赞 评论 收藏
转发
点赞 收藏 评论
分享
牛客网
牛客企业服务