从这里拿来一个比较好用的split函数。
#include <iostream> #include <vector> #include <string> #include <algorithm> using namespace std; /* SplitString来自:https://www.cnblogs.com/carsonzhu/p/5859552.html */ void SplitString(const string &s, vector<string> &v, const string &c) { string::size_type pos1, pos2; pos2 = s.find(c); pos1 = 0; while (string::npos != pos2) { v.push_back(s.substr(pos1, pos2 - pos1)); pos1 = pos2 + c.size(); pos2 = s.find(c, pos1); } if (pos1 != s.length()) v.push_back(s.substr(pos1)); } void printvec(vector<string> &vec) { for (int i = 0; i < vec.size() - 1; i++) { cout << vec[i] << ","; } cout << vec[vec.size() - 1] << endl; return; } int main() { vector<string> vec; string input; while (cin >> input) { SplitString(input, vec, ","); sort(vec.begin(), vec.end()); printvec(vec); vec.clear(); } return 0; }