C++对字符串流的读写

参考内容来自:http://c.biancheng.net/cpp/biancheng/view/261.html

C++ primer第四版P257

平台:Ubuntu,G++ compiler

头文件:#include<sstream>,

三种字符流类型:istringstream,ostringstream,stringstream,

字符串流的赋值格式有

stringstream stream(string类型);将string和stream绑定起来。

stringstream stream(缓冲数组数组首地址,缓冲数组长度);这条可参考网址上的例子

stringstream的特定操作:

stringstream strm;创建自由的stringstream

stingstream stem(s);创建存储s的副本的stringstream对象,其中s是string类型的对象

strm.str();返回strm中存储的string类型对象,也就是选取strm流中string类型的对象作为返回值

strm.str(s);将string类型的s复制给strm,返回void

代码如下:

 1 #include<iostream>
  2 #include<vector>
  3 #include<deque>
  4 #include<list>
  5 #include<stack>
  6 #include<queue>
  7 #include<algorithm>
  8 #include<string>
  9 #include<fstream>
 10 #include<sstream>
 11 using namespace std;
 12 
 13 string line1 = "We were her pride of 10 she named us:";
 14 string line2 = "Benjamin,Phoenix,the Prodigal";
 15 string line3 = "and perspicacious pacific Suzanne";
 16 
 17 string sentence = line1+' '+line2+' '+line3;
 18 
 19 int main()
 20 {
 21     vector<string>words;
 22     string word;
 23     stringstream istr(sentence);
 24     while(istr>>word)
 25             words.push_back(word);
 26     for(vector<string>::iterator iter = words.begin();iter!=words.end();++iter)
 27             cout<<*iter<<" ";
 28 
 29     return 0;
 30 }
 31 
~                                                                                                                                               
~                                                                                                                                               
~                                                                                                                                               
~                                                                                                                                               
~                                                                                                                                               
~                   

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值