【C++ Primer | 8】IO库

 istringstream的用法

 1 #include<iostream>
 2 #include <sstream>
 3 using namespace std;
 4 
 5 int main()
 6 {
 7     istringstream in;
 8     string line, str1;
 9     while(getline(cin,line)) // 从终端接受一行字符串,存入line中
10     {  
11         in.str(line);      // 把line中的字符串存入字符串流中
12         while(in >> str1)  // 每次读取一个单词(以空格为例)存入str1中;
13         {   
14             cout << str1 <<endl;
15         }
16     }
17     return 0;
18 }

输出结果:

ostringstream的用法

 1 #include <string>  
 2 #include <iostream> 
 3 #include <sstream>
 4 using namespace std;
 5 
 6 void main()
 7 {
 8     ostringstream ostr1; // 构造方式1  
 9     ostringstream ostr2("abc"); // 构造方式2  
10 
11     /*----------------------------------------------------------------------------
12     *** 方法str()将缓冲区的内容复制到一个string对象中,并返回
13     ----------------------------------------------------------------------------*/
14     ostr1 << "ostr1 " << 2012 << endl; // 格式化,此处endl也将格式化进ostr1中  
15     cout << ostr1.str();
16 
17     /*----------------------------------------------------------------------------
18     *** 建议:在用put()方法时,先查看当前put pointer的值,防止误写
19     ----------------------------------------------------------------------------*/
20     long curPos = ostr2.tellp(); //返回当前插入的索引位置(即put pointer的值),从0开始   
21     cout << "curPos = " << curPos << endl;
22 
23     ostr2.seekp(2); // 手动设置put pointer的值  
24     ostr2.put('g');     // 在put pointer的位置上写入'g',并将put pointer指向下一个字符位置  
25     cout << ostr2.str() << endl;
26 
27 
28     /*----------------------------------------------------------------------------
29     *** 重复使用同一个ostringstream对象时,建议:
30     *** 1:调用clear()清除当前错误控制状态,其原型为 void clear (iostate state=goodbit);
31     *** 2:调用str("")将缓冲区清零,清除脏数据
32     ----------------------------------------------------------------------------*/
33     ostr2.clear();
34     ostr2.str("");
35 
36     cout << ostr2.str() << endl;
37     ostr2.str("_def");
38     cout << ostr2.str() << endl;
39     ostr2 << "gggghh";    // 覆盖原有的数据,并自动增加缓冲区  
40     cout << ostr2.str() << endl;
41     ostr2.str("");   // 若不加这句则运行时错误,因为_df所用空间小于gggghh,导致读取脏数据
42     ostr2.str("_df");
43     cout << ostr2.str() << endl;
44 
45     // 输出随机内存值,危险
46     const char* buf = ostr2.str().c_str();
47     cout << buf << endl;
48 
49     // 正确输出_df
50     string ss = ostr2.str();
51     const char* buffer = ss.c_str();
52     cout << buffer << endl;
53 }

 输出结果:

stringstream的用法

例1:基本类型转换例子 int 转 string

 1 #include <string>
 2 #include <sstream>
 3 #include <iostream> 
 4 
 5 int main()
 6 {
 7     std::stringstream stream;
 8     std::string result;
 9     int i = 1000;
10     stream << i; //将int输入流
11     stream >> result; //从stream中抽取前面插入的int值
12     std::cout << result << std::endl; // print the string "1000"
13 } 

例2: 除了基本类型的转换,也支持char *的转换

 1 #include <sstream>
 2 #include <iostream> 
 3 
 4 int main()
 5 {
 6     std::stringstream stream;
 7     char result[8] ;
 8     stream << 8888; //向stream中插入8888
 9     stream >> result; //抽取stream中的值到result
10     std::cout << result << std::endl; // 屏幕显示 "8888"
11 } 

参考资料

1. string和stringstream用法总结

原文地址:https://www.cnblogs.com/sunbines/p/13811700.html