Sample Input
0051231232050775
Sample Output
0 77 12312320
#include<set>
#include<string>
#include<iostream>
#include <sstream>
using namespace std;
int main()
{
string str1 = "0051231232050775";
string tempstr;
set<int> set1;
for (int i = 0; i < str1.size(); i++)
{
if (str1[i] != '5')
{
tempstr.push_back(str1[i]);//没有遇到5,就将字符写入到临时string
}
else if(!tempstr.empty())//遇到5且临时string不为空
{
istringstream iss(tempstr);//将临时字符串加载到string输入流
int tempint;
iss >> tempint;//从string输入流获取一个int
set1.insert(tempint);
tempstr.clear();//插入到集合之后就清空
}
}
for (set<int>::iterator p = set1.begin(); p != set1.end(); p++)
{
cout << *p << ' ';
}
return 0;
}
输出结果:
0 77 12312320