ACM输入输出练习—学会即可举一反三,主要针对字符串类型分割处理
这里利用getline()和字符串流来分割字符串并格式化输出,思路大概如此。
#include <iostream>
#include <string>
#include <sstream>
#include <set>
using namespace std;
int main(){
string str;
stringstream ss;
multiset<string> st1;
while (cin >> str) {
ss << str;
string tmp
while (getline(ss, tmp, ',')) {
st1.insert(str);
}
//格式化输出
int n=st1.size();
for(auto e:st1){
cout<<e;
n--;
if(n!=0){
cout<<',';
}
}
st1.clear();
cout<<endl;
ss.clear();
}
return 0;
}
如果输入是一窜数字和逗号组合的字符串 同样可以把每个数字转为int进行下一步操作。用一个atoi() 转一下即可
int main(){
string str;
stringstream ss;
multiset<int> st1;
while (cin >> str) {
ss << str;
string tmp;
while (getline(ss, tmp, ',')) {
int a=atoi(tmp.c_str());
st1.insert(a);
}
int n=st1.size();
for(auto e:st1){
cout<<e;
n--;
if(n!=0){
cout<<',';
}
}
st1.clear();
cout<<endl;
ss.clear();
}
return 0;
}