C++的IO处理中的头文件以及类理解(2)<sstream>头文件

  头文件<sstream>中定义的类型都继承iostream头文件中定义的类型。除了继承得来的操作,sstream中定义的类型还增加了一些成员来管理与流相关联的string.

一、 <sstream>头文件

  该标准头文件中包含了ostringstream、istringstream、stringstream这三个类,要使用他们创建对象,对内存中的string对象进行io处理。

  这些类型可以向string写入数据,从string读取数据,就像string是一个IO流一样。

istringstream从string读取数据,(istringstream对象用来把一个已定字符串中的以空格、Tab隔开的内容提取出来,功能类似于C语言中的sscanf函数)只支持>>操作符,
ostringstream向string写入数据,只支持<<操作符,
stringstream既可从string读数据也可向string写数据,支持<<、>>操作符。

类:

1. stringbuf 类

  http://www.cplusplus.com/reference/sstream/stringbuf/stringbuf/

  构造一个string stream buffer (字符串流对象)。

构造函数如下:

default (1)
  1. explicit stringbuf (ios_base::openmode which = ios_base::in | ios_base::out);
initialization (2)
  1. explicit stringbuf (const string& str,
  2. ios_base::openmode which = ios_base::in | ios_base::out);
copy (3)
  1. stringbuf (const stringbuf&) = delete;
move (4)
  1. stringbuf (stringbuf&& x);

(1)空的构造函数, 默认构造函数

  构造一个 stringbuf 对象, 用一个空的序列, 参数which是 设置的open model。

(2)初始化构造函数

  用一个string 对象作为内容,来构造stringbuf 对象, 参数which 是打开模式

(3)拷贝构造函数

  应为已经deleted,舍弃,因此没有拷贝构造。

(4)移动构造函数

  获取参数stringbuf x 的内容;

  x 处于不确定但是有效的状态。

  不确定内部的sequence 是x ,或者是x 的拷贝, 但是两个都是相关依赖的序列。

参数情况:

 str, 参数string对象,内容已经拷贝;

 x,  参数stringbuf对象,其内容已经移动;

which:打开输出xx对象的模式, 即进入内部stringbuf对象,给定的字符串序列。 是一个枚举类型。任意合并值都是重要影响。

  •   os_base::in, 表示:input, 内部的字符串序列支持input 操作;
  •   ios_base::out: 表示,output, 序列支持输出操作。
  •   ios_base::ate:表示 at end, 写入位置在构造函数的后面, 并且在每次用str()成员重置内容后。
  •   其他类型的ios_base::openmode, 例如: ios_base::app. 注意, ios_base::out ,总是设置ostringsteam 对象, 尽管没有显示设置参数which。 注意, ostringsteam是一个输出steam, 其内部的stringbuf对象可能设置支持input 操作的。

测试程序:

  1. // stringbuf example
  2. #include <iostream> // std::cout, std::ostream, std::hex
  3. #include <sstream> // std::stringbuf
  4. #include <string> // std::string
  5.  
  6. int main ()
  7. {
  8. std::stringbuf buffer; // empty stringbuf

  9.   
     //将 stringbuf 对象与 输出流关联,这样,可以利用输出输入,对stringbuf对象进行赋值
  10. std::ostream os (&buffer); // associate stream buffer to stream
  11.  
  12. // mixing output to buffer with inserting to associated stream:
     //函数sputn(),
  1.  
  1. buffer.sputn ("255 in hexadecimal: ",);
  2. os << std::hex << ;
  3.  
  4. std::cout << buffer.str();// 拷贝当前stringbuf 对象内内容,并以string对象返回。
  5.  
  6. return ;
  7. }

输出

  1. 255 in hexadecimal: ff

注意:

streambuf::sputn()函数原型

  1. streamsize sputn (const char* s, streamsize n);
    作用:
      将字符序列写入到streambuf内部的内容中。
      
    参数:
      s:指向字符串序列的指针,用于将要写入stringbuf的内容。
      n:将要写入字符的长度, 非负值, streamsize是符号整数类型。
    返回:
      已经写入字符的数量
  1. // sputn() example
  2. #include <iostream> // std::streambuf
  3. #include <fstream> // std::ofstream
  4.  
  5. int main () {
      //字符序列,字符数组,指针
  6. const char sentence[]= "Sample sentence";//注意,末尾有字符串终止符号1个字符
  7.  
  8. std::ofstream ostr ("test.txt");
  9. if (ostr) {
  10. std::streambuf * pbuf = ostr.rdbuf();
      //直接用streambuf进行直接输入内容
  11. pbuf->sputn (sentence,sizeof(sentence)-);
  12. ostr.close();
  13. }
  14.  
  15. return ;
  16. }

2. . 测试

  

ostringstream::ostringstream()构造函数

测试:

  1. // ostringstream constructor
  2. #include <iostream> // std::cout, std::ios
  3. #include <sstream> // std::ostringstream
  4.  
  5. int main () {
  6. std::ostringstream foo; // out
  7. std::ostringstream bar (std::ostringstream::ate); // out|ate
  8.  
  9. foo.str("Test string");
  10. bar.str("Test string");
  11.  
  12. foo << ;
  13. bar << ;
  14.  
  15. std::cout << foo.str() << '\n';
  16. std::cout << bar.str() << '\n';
  17. return ;
  18. }

输出:

101t string

Test string101

  ostringstream, istringstream,stringstream类型的用法程序

  1. #include "sstream.hpp"
  2. #include <iostream>
  3. #include <sstream> // ostringstream/istringstream/stringstream
  4. #include <string>
  5.  
  6. // reference: http://www.cplusplus.com/reference/sstream/ostringstream/
  7. int test_ostringstream()
  8. {
  9. // ostringstream: Output stream class to operate on strings
  10. // 1. rdbuf: Returns a pointer to the internal stringbuf object
  11. std::ostringstream oss1;
  12. // using stringbuf directly 直接使用内部的stringbuf来进行输出,用函数
  13. std::stringbuf *pbuf = oss1.rdbuf();
  14. pbuf->sputn("Sample string", );//输入字符个数13的一个字符数组。
  15. std::cout << pbuf->str() << std::endl;//获取输出内容的字符串形式
  16.  
  17. // 2. str(): 返回当前strem对象内的内容拷贝的数据,以字符串返回。returns a string object with a copy of the current contents of the stream
  18. // str(const string& s): 删除string对象之前的内容, 并设置为与steam对象内的内容一样的数据。sets s as the contents of the stream, discarding any previous contents.
  19. // The object preserves its open mode: if this includes ios_base::ate,注意,如果包含ios_base::ate模式,则对象讲标六之前的内容, 写入的位置将会移动到new sequence的末尾。
  20. // the writing position is moved to the end of the new sequence
  21. std::ostringstream oss2;
  22. oss2 << "One hundred and one: " << ;
  23. std::string s1 = oss2.str();
  24. std::cout << s1 << '\n';
  25.  
  26. // 3. swap: c++11, Exchanges all internal data between x and *this
      // 将参数ostringsteam 的内用与当前ostringsteam内容互换。
  27. std::ostringstream foo;
  28. std::ostringstream bar;
  29. foo << ;
  30. bar << ;
  31. foo.swap(bar);
  32. std::cout << "foo: " << foo.str() << '\n';
  33. std::cout << "bar: " << bar.str() << '\n';
  34.  
  35. // 4. swap: Exchanges the values of the ostringstream objects x and y
  36. std::ostringstream foo2;
  37. std::ostringstream bar2;
  38. foo2 << ;
  39. bar2 << ;
  40. std::swap(foo2, bar2); // unqualified (uses argument-dependent lookup)
  41. std::cout << "foo2: " << foo2.str() << '\n';
  42. std::cout << "bar2: " << bar2.str() << '\n';
  43.  
  44. // 5. ostringstream constructor: Construct an object and optionally initialize its content
  45. // explicit ostringstream ( openmode which = ios_base::out );
  46. // explicit ostringstream ( const string & str, openmode which = ios_base::out );
  47. std::ostringstream foo3; // out, 默认的
  48. std::ostringstream bar3(std::ostringstream::ate); // out|ate, 这是追加模式,任何后续的输入,到从streambuf的末尾追加。
  49. foo3.str("Test string"); // str: sets s as the contents of the stream, discarding any previous contents
  50. bar3.str("Test string");
  51. foo3 << ;
  52. bar3 << ;
  53. std::cout << foo3.str() << '\n'; // 101t string
  54. std::cout << bar3.str() << '\n'; // Test string101
  55.  
  56. std::string s{ "abcde" };
  57. std::ostringstream foo4(s); // 创建存储s的副本的ostringstream对象
  58. std::cout << "foo4: " << foo4.str() << std::endl;
  59.  
  60. // reference: https://latedev.wordpress.com/2011/11/16/c-stringstreams/
  61. std::ostringstream os;
  62. os << "the ";
  63. os << "quick ";
  64. os << "brown ";
  65. os << "fox";
  66. std::string s2 = os.str();
  67. std::cout << s2 << std::endl;
  68.  
  69. // double to string ==> c++11 to_string
  70. double d = 123.45;
  71. std::ostringstream os3;
  72. os3 << d;
  73. std::string s3 = "The value of d is " + os3.str();
  74. std::cout << s3 << std::endl;
  75.  
  76. return ;
  77. }
  78.  
  79. // reference: http://www.cplusplus.com/reference/sstream/istringstream/
  80. int test_istringstream()
  81. {
  82. // istringstream: Input stream class to operate on strings 输入流类来操作string类型
  83. // 1. istringstream constructor
  84. std::istringstream is("the quick brown fox");
  85. std::string s;
  86. while (is >> s) {//直接用输入流来设置string
  87. std::cout << s << std::endl;
  88. }
  89.  
  90. std::string stringvalues = "125 320 512 750 333";
  91. std::istringstream iss6(stringvalues);//用字符串设置输入流
  92. for (int n = ; n<; n++) {
  93. int val;
  94. // Elements in a character stream are considered to be separated by 'white space'
  95. // which is basically space, tab and newline characters,空格符,tab, 下一行的符号,都来表示分隔符
  96. iss6 >> val;
  97. std::cout << val * << '\n';
  98. }
  99.  
  100. // 2. rdbuf: Returns a pointer to the internal stringbuf object, with which the object was associated on construction
      //返回指针,指向内部的stringbuf对象, 这是早stringsteam流在创建的时候关联的。
  101. std::istringstream iss;
  102. std::stringbuf *pbuf = iss.rdbuf();
  103.  
  104. // using stringbuf directly:直接用stringbuf作为输入,最终表现仍然是istringsteam对象
  105. pbuf->str("Example string");
  106.  
  107. int size = pbuf->in_avail();
  108. while (pbuf->in_avail()>)
  109. std::cout << static_cast<char>(pbuf->sbumpc());
  110. std::cout << std::endl;
  111.  
  112. // 3. str(): returns a string object with a copy of the current contents of the stream
  113. // str(const string& s): sets str as the contents of the stream, discarding any previous contents.
  114. // The object preserves its open mode: if this includes ios_base::ate,
  115. // the writing position is moved to the end of the new sequence
  116. std::istringstream iss2;
  117. std::string strvalues = "32 240 2 1450";
  118. iss2.str(strvalues);//用参数stirng类设置istringsteam的内容。
  119.  
  120. for (int n = ; n<; n++) {
  121. int val;
  122. // Elements in a character stream are considered to be separated by 'white space'
  123. // which is basically space, tab and newline characters
  124. iss2 >> val;//依次输出到变量string
  125. std::cout << val << '\n';
  126. }
  127. std::cout << "Finished writing the numbers in: ";
  128. std::cout << iss2.str() << '\n';
  129.  
  130. // 4. swap: c++11, Exchanges all internal data between x and *this.
  131. std::istringstream foo("");
  132. std::istringstream bar("");
  133.  
  134. foo.swap(bar);
  135.  
  136. int val;
  137. foo >> val; std::cout << "foo: " << val << '\n'; //
  138. bar >> val; std::cout << "bar: " << val << '\n'; // 100
  139.  
  140. // 5. swap: Exchanges the values of the istringstream objects x and y
  141. std::istringstream foo2("");
  142. std::istringstream bar2("");
  143.  
  144. swap(foo2, bar2); // unqualified (uses argument-dependent lookup)
  145. int val2;
  146. foo2 >> val2; std::cout << "foo2: " << val2<< '\n'; //
  147. bar2 >> val2; std::cout << "bar2: " << val2 << '\n'; //
  148.  
  149. return ;
  150. }
  151.  
  152. // reference: http://www.cplusplus.com/reference/sstream/stringstream/
  153. int test_stringstream()
  154. {
  155. // 1. stringstream: Stream class to operate on strings
  156. std::stringstream ss;//即处理输入字符串,有处理输出字符串
  157. ss << << ' ' << ;
  158.  
  159. int foo, bar;
  160. ss >> foo >> bar;
  161.  
  162. std::cout << "foo: " << foo << '\n'; //
  163. std::cout << "bar: " << bar << '\n'; // 200
  164.  
  165. // 2. rdbuf: Returns a pointer to the internal stringbuf object, with which the object was associated on construction
  166. std::stringstream ss2;
  167.  
  168. // using stringbuf directly:
  169. std::stringbuf *pbuf = ss2.rdbuf();
  170. pbuf->sputn("Example string", );//输入 13个字符的字符串
  171.  
  172. char buffer[];
  173. pbuf->sgetn(buffer, );//获取80个字符的string buffer
  174.  
  175. std::cout << buffer << std::endl;
  176.  
  177. // 3. str(): returns a string object with a copy of the current contents of the stream
  178. // str(const string& s): sets s as the contents of the stream, discarding any previous contents.
  179. // The object preserves its open mode: if this includes ios_base::ate,
  180. // the writing position is moved to the end of the new sequence
  181. std::stringstream ss3;
  182. ss3.str("Example string");// str()函数作为内容输入
  183. std::string s3 = ss3.str();//输出内部内容,以字符串形式
  184. std::cout << s3 << '\n';
  185.  
  186. // 4.1 swap: c++11, Exchanges all internal data between x and *this
  187. std::stringstream foo4;
  188. std::stringstream bar4;
  189.  
  190. foo4 << ;
  191. bar4 << ;
  192.  
  193. foo4.swap(bar4);
  194. int val;
  195. foo4 >> val; std::cout << "foo4: " << val << '\n'; //
  196. bar4 >> val; std::cout << "bar4: " << val << '\n'; // 100
  197.  
  198. // 4.2 swap(stringstream): Exchanges the values of the stringstream objects x and y
  199. std::stringstream foo5;
  200. std::stringstream bar5;
  201.  
  202. foo5 << ;
  203. bar5 << ;
  204.  
  205. std::swap(foo5, bar5);
  206. int val5;
  207. foo5 >> val5; std::cout << "foo5: " << val5 << '\n'; //
  208. bar5 >> val5; std::cout << "bar5: " << val5 << '\n'; //
  209.  
  210. return ;
  211. }

3. C++还提供了另一个头文件<strsteam>,其功能和<ssteam>相似,是为了兼容c 类型的字符串(字符数组)而提出来的。

  <strstream> 中istrstream,ostrstream,strstream 也有对应的三个类型。

istrstream类用于执行C风格的串流的输入操作,也就是以字符数组作为输入设备。 
ostrstream类用于执行C风格的串流的输出操作,也就是一字符数组作为输出设备。 
strstream类同时可以支持C风格的串流的输入输出操作。

  注意:

  strstream里的东西已经被c++标准明确标明为“不要再使用”

endl;

C++的IO处理中的头文件以及类理解(2)<sstream>头文件的更多相关文章

  1. MFC中文件对话框类CFileDialog详解及文件过滤器说明

    当前位置 : 首页 » 文章分类 :  开发  »  MFC中文件对话框类CFileDialog详解及文件过滤器说明 上一篇 利用OpenCV从摄像头获得图像的坐标原点是在左下角 下一篇 Word中为 ...

  2. php加了命名空间没引入初始化文件:类的命名空间要与文件夹名一致namespace Business\Event;缺少了Event

    php加了命名空间没引入初始化文件:类的命名空间要与文件夹名一致namespace Business\Event;缺少了Event

  3. C++的IO处理中的头文件以及类理解(1)

    C++语言不直接处理输入输出,而是通过一簇定义在标准库中的类型来处理IO.这些类型支持从设备读取数据.向设备写入数据的IO操作,设备可以是文件.控制台窗口等,还有一些类型允许内存IO,即,从strin ...

  4. idea中如何将单个java类导出为jar包文件?

    idea作为一个java开发的便利IDE工具,个人是比较喜欢的,今天来探索个小功能:  导出单个类文件为jar包! 偶有这种需求,就是某个类文件独立存在,但是需要将其导出为jar,供别人临时使用,或者 ...

  5. io流中的装饰模式对理解io流的重要性

    为了说明 io流中的装饰者模式对理解io流的重要性,我想先简要介绍以下io的装饰模式. 装饰(decorator)你也可以翻译成修饰.比如:一个会精通化学数学的物理学家.在这个"物理学家&q ...

  6. IO包中的RandomAccessFile类

    RandomAccessFile RandomAccessFile 是随机访问文件的类.它支持对文件随机访问的读取和写入,即我们也可以从指定的位置读取/写入文件数据,因为该类在其内部封装了一个数组和指 ...

  7. C++文件流类与文件流对象

    文件流是以外存文件为输入输出对象的数据流.输出文件流是从内存流向外存文件的数据,输入文件流是从外存文件流向内存的数据.每一个文件流都有一个内存缓冲区与之对应. 请区分文件流与文件的概念,不用误以为文件 ...

  8. C++学习47 文件的概念 文件流类与文件流对象 文件的打开与关闭

    迄今为止,我们讨论的输入输出是以系统指定的标准设备(输入设备为键盘,输出设备为显示器)为对象的.在实际应用中,常以磁盘文件作为对象.即从磁盘文件读取数据,将数据输出到磁盘文件.磁盘是计算机的外部存储器 ...

  9. [19/03/29-星期五] IO技术_File(文件)类(可操作文件,不能操作其里边内容,位于Java.io 包中)&递归遍历

    一.概念 java.io.File类:代表文件和目录. 在开发中,读取文件.生成文件.删除文件.修改文件的属性时经常会用到本类. 以pathname为路径创建File对象,如果pathname是相对路 ...

随机推荐

  1. Java相关查询记录

    Version of Spring Facet could not be detected. http://yijiesuifeng.iteye.com/blog/2221444

  2. 一些用Css实现的效果

    今天写一个笔试题,其中有一个是用css实现直角梯形的效果,我给出的答案是: <style> .wrap{ width: 100px; height: 50px; border-top:90 ...

  3. I/O多路复用、协程、线程、进程

    select注册fd,阻塞,当有fd状态改变时返回,确认对应的fd,做下一步处理.简单来说就是先注册,注册完后休眠并设置一个定时器醒来查看,有事件就通知来取,进行后续动作,没事件就继续睡,再设闹钟.用 ...

  4. trunk端口配置错误导致环路

    端口下 switchport mode trunk spannning-tree portfast 上述两个命令同时执行将导致环路

  5. jQuery index() 方法

    比如同一级有多个li,获得点击的元素的下标,确定第几个. $("li").click(function(){alert($(this).index());});

  6. java多线程管理 concurrent包用法详解

        我们都知道,在JDK1.5之前,Java中要进行业务并发时,通常需要有程序员独立完成代码实现,当然也有一些开源的框架提供了这些功能,但是这些依然没有JDK自带的功能使用起来方便.而当针对高质量 ...

  7. windows 下安装redis

    https://github.com/MicrosoftArchive/redis/releases redis 服务安装到系统 redis-server.exe --service-install ...

  8. 探索未知种族之osg类生物---渲染遍历之裁剪三

    前言 在osgUtil::CullVisitor,我们发现apply函数的重载中,有CullVisitor::apply(Group& node),CullVisitor::apply(Swi ...

  9. 探索未知种族之osg类生物---渲染遍历之器官协作

    好了,现在我们经过三节的介绍我们已经大体上明确了单线程模型(SingleThreaded)下 OSG 渲染遍历的工作流程.事实上无论是场景的筛选render还是绘制cull工作,最后都要归结到场景视图 ...

  10. Android学习(三)

    学号 20189214 <Android程序开发>第八周学习总结 教材学习内容总结 GridView GridView和ListView一样是AbsListView的子类; 都需要一个Ad ...