//// cin.get == '\n' #include <iostream> #include <sstream> #include <algorithm> using namespace std; int main(){ string tmp; vector<string> res; while(cin >> tmp) { //双指针法分割字符串 for(int i = 0;i < tmp.size(); i++) { int j = i; while(tmp[j] != ',' && j < tmp.size()) j++; res.push_back(tmp.substr(i,j - i)); i = j; } if(cin.get() == '\n') { sort(res.begin(),res.end()); for(int i = 0 ; i < res.size() - 1; i++) cout << res[i] << ','; cout << res[res.size() - 1] <<endl; res.clear(); } } return 0; } //// getline() + stringstream #include <iostream> #include <sstream> #include <algorithm> using namespace std; int main(){ string str; while (getline(cin, str)){ // str - > ss - > s stringstream ss; ss << str; string s; vector<string> res; //以逗号对ss进行分割 while (getline(ss, s, ',')) res.push_back(s); //排序 sort(res.begin(), res.end()); //输出 for(int i = 0 ; i < res.size() - 1; i++) cout << res[i] << ','; cout << res[res.size() - 1] <<endl; } return 0; }
全部评论
(0) 回帖