c++输入输出练习如何处理逗号分隔符

 

 

考试过程中,遇到过这种情况,直接蒙了。

这是别人的代码

 1 #include <iostream>
 2 #include <sstream>
 3 #include <string>
 4 #include <vector>
 5 #include <algorithm>
 6 
 7 using namespace std;
 8 
 9 int main()
10 {
11     string line;
12     string tmp_str;
13     vector <string> v_str;
14     
15     while (getline(cin, line))
16     {
17         stringstream sstream(line);
18         
19         while (getline(sstream, tmp_str, ','))
20         {
21             v_str.push_back(tmp_str);
22         }
23         
24         sort(v_str.begin(), v_str.end());
25         
26         for (auto i = v_str.begin(); i != v_str.end(); ++i)
27         {
28             cout << *i;
29             if (i + 1 != v_str.end())
30             {
31                 cout << ',';
32             }
33         }
34         cout << endl;
35         v_str.clear();
36     }
37     
38     return 0;
39 }


 

posted @ 2022-02-14 22:44  技术笔记记录  阅读(761)  评论(0)    收藏  举报