C++ ACM模式:输入输出几组带逗号字符串排序练习

题目:

对输入的字符串进行排序后输出

输入

a,c,bb
f,dddd
nowcoder

输出

a,bb,c
dddd,f
nowcoder

代码:

#include<algorithm>
#include<vector>
#include<iostream>
#include<sstream>
using namespace std;

int main() {
    string input;
    while(getline(cin, input)) {
        vector<string> res;
        stringstream s(input);
        string tmp;
        while(getline(s,tmp,',')) {
            res.push_back(tmp);
        }
        sort(res.begin(), res.end());
        cout << res[0];
        for(int i = 1; i < res.size(); i++) {
            cout << "," <<res[i];
        }
        res.clear();
        cout << endl;
    }
    return 0;
}

用到了stringstream和getline匹配 读取用逗号分割开的字符串

猜你喜欢

转载自blog.csdn.net/qq_44189622/article/details/130616464