赞
踩
目录
stringstream 数据库 <sstream>
- string x="abcdefg";
- stringstream ss;
- ss<<x;
2.字符串初始化(一般用这个方便很多)
-
- string x="abcdefg";
- stringstream ss(x);
调用str()函数 str()函数可以将其他类型的数据转换为字符串类型,从而方便我们输出和处理数据
cout<<ss.str()<<endl;
- #include<iostream>
- #include<sstream>
- using namespace std;
- int main()
- {
- string x="abcdefgh";
- //初始化
- stringstream ss(x);
- cout<<ss.str()<<endl;
- //修改字符串
- ss.str("1234565");
- cout<<ss.str()<<endl;
- //清空字符串
- //ss.clear();
- ss.str(" ");
- cout<<ss.str()<<endl;
- cout<<"0"<<endl;
- return 0;
- }

- #include<iostream>
- #include<sstream>
- using namespace std;
- int main()
- {
- string x="a b c d efg h j";
- stringstream ss(x);
- string s;
- while(ss>>s)
- {
- cout<<s<<endl;
- }
- return 0;
- }
借用getline()函数
- #include<iostream>
- #include<sstream>
- using namespace std;
- int main()
- {
- string x="a, b, c,d,efg,h,j";
- stringstream ss(x);
- string s;
- while(getline(ss,s,','))
- {
- cout<<s<<endl;
- }
- return 0;
- }
- #include<iostream>
- #include<sstream>
- using namespace std;
- int main()
- {
- string x="12345678";
- stringstream ss(x);
- int p;
- ss>>p;//就想象成读入给p p就有值了(从ss中提取数据 输入到x中)
- cout<<p<<endl;
- cout<<p/2<<endl;
- return 0;
- }
>>是流提取符 <<是流插入符
- #include<iostream>
- #include<sstream>
- using namespace std;
- int main()
- {
- string x="12 34 56 78";
- stringstream ss(x);
- int p;
- while(ss>>p)
- {
- cout<<p<<endl;
- cout<<"*** "<<p/2<<endl;
- }
- return 0;
- }
- #include<iostream>
- #include<cstring>
- #include<sstream>
- #include<algorithm>
- using namespace std;
- int main()
- {
- stringstream str;
- string sstr;
- int x=99;
- str<<x;//<<流插入 将x中的数据插入到str中
- cout<<str.str();//str>>sstr
- return 0;
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。