赞
踩
void StrtoInt(string& str)
{
stringstream ss;
ss << str;
//int num = ss.get();
int num;
ss >> num;
cout << num * 2 << endl;
}
void InttoStr(int num)
{
stringstream ss;
ss << num;
string s;
ss >> s;
s.append("that is int to string");
cout << s << endl;
}
这个操作的情况下,可以完成一些小的统计:从0到2020中9出现的数字有多少个?
int Func2()
{
int num = 0;
for (int i = 0; i < 2021; i++)
{
stringstream s;
s << i;
string str = s.str();
if (str.find('9') != string::npos)
num++;
}
return a;
}
void StrJoin()
{
string str1 = "hello ";
string str2 = "world!";
stringstream ss;
ss << str1 << str2;
cout << ss.str() << endl;
}
void StrSplit()
{
string str = "hello world !";
stringstream ss;
ss << str;
string s1, s2, s3;
ss >> s1 >> s2 >> s3;
cout << s1 << endl;
cout << s2 << endl;
cout << s3 << endl;
}
stringstream的基本用法已经讲完了,在进行一些数据转化和数据分割的时候可以减少很多的时间。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。