C++的IO处理中的头文件以及类理解(2)<sstream>头文件
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)
- explicit stringbuf (ios_base::openmode which = ios_base::in | ios_base::out);
initialization (2)
- explicit stringbuf (const string& str,
- ios_base::openmode which = ios_base::in | ios_base::out);
copy (3)
- stringbuf (const stringbuf&) = delete;
move (4)
- 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 操作的。
测试程序:
- // stringbuf example
- #include <iostream> // std::cout, std::ostream, std::hex
- #include <sstream> // std::stringbuf
- #include <string> // std::string
- int main ()
- {
- std::stringbuf buffer; // empty stringbuf
//将 stringbuf 对象与 输出流关联,这样,可以利用输出输入,对stringbuf对象进行赋值。- std::ostream os (&buffer); // associate stream buffer to stream
- // mixing output to buffer with inserting to associated stream:
//函数sputn(),
- buffer.sputn ("255 in hexadecimal: ",);
- os << std::hex << ;
- std::cout << buffer.str();// 拷贝当前stringbuf 对象内内容,并以string对象返回。
- return ;
- }
输出
- 255 in hexadecimal: ff
注意:
streambuf::sputn()函数原型
- streamsize sputn (const char* s, streamsize n);
作用:
将字符序列写入到streambuf内部的内容中。
参数:
s:指向字符串序列的指针,用于将要写入stringbuf的内容。
n:将要写入字符的长度, 非负值, streamsize是符号整数类型。
返回:
已经写入字符的数量
- // sputn() example
- #include <iostream> // std::streambuf
- #include <fstream> // std::ofstream
- int main () {
//字符序列,字符数组,指针- const char sentence[]= "Sample sentence";//注意,末尾有字符串终止符号1个字符
- std::ofstream ostr ("test.txt");
- if (ostr) {
- std::streambuf * pbuf = ostr.rdbuf();
//直接用streambuf进行直接输入内容- pbuf->sputn (sentence,sizeof(sentence)-);
- ostr.close();
- }
- return ;
- }
2. . 测试
ostringstream::ostringstream()构造函数
测试:
- // ostringstream constructor
- #include <iostream> // std::cout, std::ios
- #include <sstream> // std::ostringstream
- int main () {
- std::ostringstream foo; // out
- std::ostringstream bar (std::ostringstream::ate); // out|ate
- foo.str("Test string");
- bar.str("Test string");
- foo << ;
- bar << ;
- std::cout << foo.str() << '\n';
- std::cout << bar.str() << '\n';
- return ;
- }
输出:
101t string
Test string101
ostringstream, istringstream,stringstream类型的用法程序
- #include "sstream.hpp"
- #include <iostream>
- #include <sstream> // ostringstream/istringstream/stringstream
- #include <string>
- // reference: http://www.cplusplus.com/reference/sstream/ostringstream/
- int test_ostringstream()
- {
- // ostringstream: Output stream class to operate on strings
- // 1. rdbuf: Returns a pointer to the internal stringbuf object
- std::ostringstream oss1;
- // using stringbuf directly 直接使用内部的stringbuf来进行输出,用函数
- std::stringbuf *pbuf = oss1.rdbuf();
- pbuf->sputn("Sample string", );//输入字符个数13的一个字符数组。
- std::cout << pbuf->str() << std::endl;//获取输出内容的字符串形式
- // 2. str(): 返回当前strem对象内的内容拷贝的数据,以字符串返回。returns a string object with a copy of the current contents of the stream
- // str(const string& s): 删除string对象之前的内容, 并设置为与steam对象内的内容一样的数据。sets s as the contents of the stream, discarding any previous contents.
- // The object preserves its open mode: if this includes ios_base::ate,注意,如果包含ios_base::ate模式,则对象讲标六之前的内容, 写入的位置将会移动到new sequence的末尾。
- // the writing position is moved to the end of the new sequence
- std::ostringstream oss2;
- oss2 << "One hundred and one: " << ;
- std::string s1 = oss2.str();
- std::cout << s1 << '\n';
- // 3. swap: c++11, Exchanges all internal data between x and *this
// 将参数ostringsteam 的内用与当前ostringsteam内容互换。- std::ostringstream foo;
- std::ostringstream bar;
- foo << ;
- bar << ;
- foo.swap(bar);
- std::cout << "foo: " << foo.str() << '\n';
- std::cout << "bar: " << bar.str() << '\n';
- // 4. swap: Exchanges the values of the ostringstream objects x and y
- std::ostringstream foo2;
- std::ostringstream bar2;
- foo2 << ;
- bar2 << ;
- std::swap(foo2, bar2); // unqualified (uses argument-dependent lookup)
- std::cout << "foo2: " << foo2.str() << '\n';
- std::cout << "bar2: " << bar2.str() << '\n';
- // 5. ostringstream constructor: Construct an object and optionally initialize its content
- // explicit ostringstream ( openmode which = ios_base::out );
- // explicit ostringstream ( const string & str, openmode which = ios_base::out );
- std::ostringstream foo3; // out, 默认的
- std::ostringstream bar3(std::ostringstream::ate); // out|ate, 这是追加模式,任何后续的输入,到从streambuf的末尾追加。
- foo3.str("Test string"); // str: sets s as the contents of the stream, discarding any previous contents
- bar3.str("Test string");
- foo3 << ;
- bar3 << ;
- std::cout << foo3.str() << '\n'; // 101t string
- std::cout << bar3.str() << '\n'; // Test string101
- std::string s{ "abcde" };
- std::ostringstream foo4(s); // 创建存储s的副本的ostringstream对象
- std::cout << "foo4: " << foo4.str() << std::endl;
- // reference: https://latedev.wordpress.com/2011/11/16/c-stringstreams/
- std::ostringstream os;
- os << "the ";
- os << "quick ";
- os << "brown ";
- os << "fox";
- std::string s2 = os.str();
- std::cout << s2 << std::endl;
- // double to string ==> c++11 to_string
- double d = 123.45;
- std::ostringstream os3;
- os3 << d;
- std::string s3 = "The value of d is " + os3.str();
- std::cout << s3 << std::endl;
- return ;
- }
- // reference: http://www.cplusplus.com/reference/sstream/istringstream/
- int test_istringstream()
- {
- // istringstream: Input stream class to operate on strings 输入流类来操作string类型
- // 1. istringstream constructor
- std::istringstream is("the quick brown fox");
- std::string s;
- while (is >> s) {//直接用输入流来设置string
- std::cout << s << std::endl;
- }
- std::string stringvalues = "125 320 512 750 333";
- std::istringstream iss6(stringvalues);//用字符串设置输入流
- for (int n = ; n<; n++) {
- int val;
- // Elements in a character stream are considered to be separated by 'white space'
- // which is basically space, tab and newline characters,空格符,tab, 下一行的符号,都来表示分隔符
- iss6 >> val;
- std::cout << val * << '\n';
- }
- // 2. rdbuf: Returns a pointer to the internal stringbuf object, with which the object was associated on construction
//返回指针,指向内部的stringbuf对象, 这是早stringsteam流在创建的时候关联的。- std::istringstream iss;
- std::stringbuf *pbuf = iss.rdbuf();
- // using stringbuf directly:直接用stringbuf作为输入,最终表现仍然是istringsteam对象
- pbuf->str("Example string");
- int size = pbuf->in_avail();
- while (pbuf->in_avail()>)
- std::cout << static_cast<char>(pbuf->sbumpc());
- std::cout << std::endl;
- // 3. str(): returns a string object with a copy of the current contents of the stream
- // str(const string& s): sets str as the contents of the stream, discarding any previous contents.
- // The object preserves its open mode: if this includes ios_base::ate,
- // the writing position is moved to the end of the new sequence
- std::istringstream iss2;
- std::string strvalues = "32 240 2 1450";
- iss2.str(strvalues);//用参数stirng类设置istringsteam的内容。
- for (int n = ; n<; n++) {
- int val;
- // Elements in a character stream are considered to be separated by 'white space'
- // which is basically space, tab and newline characters
- iss2 >> val;//依次输出到变量string
- std::cout << val << '\n';
- }
- std::cout << "Finished writing the numbers in: ";
- std::cout << iss2.str() << '\n';
- // 4. swap: c++11, Exchanges all internal data between x and *this.
- std::istringstream foo("");
- std::istringstream bar("");
- foo.swap(bar);
- int val;
- foo >> val; std::cout << "foo: " << val << '\n'; //
- bar >> val; std::cout << "bar: " << val << '\n'; // 100
- // 5. swap: Exchanges the values of the istringstream objects x and y
- std::istringstream foo2("");
- std::istringstream bar2("");
- swap(foo2, bar2); // unqualified (uses argument-dependent lookup)
- int val2;
- foo2 >> val2; std::cout << "foo2: " << val2<< '\n'; //
- bar2 >> val2; std::cout << "bar2: " << val2 << '\n'; //
- return ;
- }
- // reference: http://www.cplusplus.com/reference/sstream/stringstream/
- int test_stringstream()
- {
- // 1. stringstream: Stream class to operate on strings
- std::stringstream ss;//即处理输入字符串,有处理输出字符串
- ss << << ' ' << ;
- int foo, bar;
- ss >> foo >> bar;
- std::cout << "foo: " << foo << '\n'; //
- std::cout << "bar: " << bar << '\n'; // 200
- // 2. rdbuf: Returns a pointer to the internal stringbuf object, with which the object was associated on construction
- std::stringstream ss2;
- // using stringbuf directly:
- std::stringbuf *pbuf = ss2.rdbuf();
- pbuf->sputn("Example string", );//输入 13个字符的字符串
- char buffer[];
- pbuf->sgetn(buffer, );//获取80个字符的string buffer
- std::cout << buffer << std::endl;
- // 3. str(): returns a string object with a copy of the current contents of the stream
- // str(const string& s): sets s as the contents of the stream, discarding any previous contents.
- // The object preserves its open mode: if this includes ios_base::ate,
- // the writing position is moved to the end of the new sequence
- std::stringstream ss3;
- ss3.str("Example string");// str()函数作为内容输入
- std::string s3 = ss3.str();//输出内部内容,以字符串形式
- std::cout << s3 << '\n';
- // 4.1 swap: c++11, Exchanges all internal data between x and *this
- std::stringstream foo4;
- std::stringstream bar4;
- foo4 << ;
- bar4 << ;
- foo4.swap(bar4);
- int val;
- foo4 >> val; std::cout << "foo4: " << val << '\n'; //
- bar4 >> val; std::cout << "bar4: " << val << '\n'; // 100
- // 4.2 swap(stringstream): Exchanges the values of the stringstream objects x and y
- std::stringstream foo5;
- std::stringstream bar5;
- foo5 << ;
- bar5 << ;
- std::swap(foo5, bar5);
- int val5;
- foo5 >> val5; std::cout << "foo5: " << val5 << '\n'; //
- bar5 >> val5; std::cout << "bar5: " << val5 << '\n'; //
- return ;
- }
3. C++还提供了另一个头文件<strsteam>,其功能和<ssteam>相似,是为了兼容c 类型的字符串(字符数组)而提出来的。
<strstream> 中istrstream,ostrstream,strstream 也有对应的三个类型。
istrstream类用于执行C风格的串流的输入操作,也就是以字符数组作为输入设备。
ostrstream类用于执行C风格的串流的输出操作,也就是一字符数组作为输出设备。
strstream类同时可以支持C风格的串流的输入输出操作。
注意:
strstream里的东西已经被c++标准明确标明为“不要再使用”
endl;
C++的IO处理中的头文件以及类理解(2)<sstream>头文件的更多相关文章
- MFC中文件对话框类CFileDialog详解及文件过滤器说明
当前位置 : 首页 » 文章分类 : 开发 » MFC中文件对话框类CFileDialog详解及文件过滤器说明 上一篇 利用OpenCV从摄像头获得图像的坐标原点是在左下角 下一篇 Word中为 ...
- php加了命名空间没引入初始化文件:类的命名空间要与文件夹名一致namespace Business\Event;缺少了Event
php加了命名空间没引入初始化文件:类的命名空间要与文件夹名一致namespace Business\Event;缺少了Event
- C++的IO处理中的头文件以及类理解(1)
C++语言不直接处理输入输出,而是通过一簇定义在标准库中的类型来处理IO.这些类型支持从设备读取数据.向设备写入数据的IO操作,设备可以是文件.控制台窗口等,还有一些类型允许内存IO,即,从strin ...
- idea中如何将单个java类导出为jar包文件?
idea作为一个java开发的便利IDE工具,个人是比较喜欢的,今天来探索个小功能: 导出单个类文件为jar包! 偶有这种需求,就是某个类文件独立存在,但是需要将其导出为jar,供别人临时使用,或者 ...
- io流中的装饰模式对理解io流的重要性
为了说明 io流中的装饰者模式对理解io流的重要性,我想先简要介绍以下io的装饰模式. 装饰(decorator)你也可以翻译成修饰.比如:一个会精通化学数学的物理学家.在这个"物理学家&q ...
- IO包中的RandomAccessFile类
RandomAccessFile RandomAccessFile 是随机访问文件的类.它支持对文件随机访问的读取和写入,即我们也可以从指定的位置读取/写入文件数据,因为该类在其内部封装了一个数组和指 ...
- C++文件流类与文件流对象
文件流是以外存文件为输入输出对象的数据流.输出文件流是从内存流向外存文件的数据,输入文件流是从外存文件流向内存的数据.每一个文件流都有一个内存缓冲区与之对应. 请区分文件流与文件的概念,不用误以为文件 ...
- C++学习47 文件的概念 文件流类与文件流对象 文件的打开与关闭
迄今为止,我们讨论的输入输出是以系统指定的标准设备(输入设备为键盘,输出设备为显示器)为对象的.在实际应用中,常以磁盘文件作为对象.即从磁盘文件读取数据,将数据输出到磁盘文件.磁盘是计算机的外部存储器 ...
- [19/03/29-星期五] IO技术_File(文件)类(可操作文件,不能操作其里边内容,位于Java.io 包中)&递归遍历
一.概念 java.io.File类:代表文件和目录. 在开发中,读取文件.生成文件.删除文件.修改文件的属性时经常会用到本类. 以pathname为路径创建File对象,如果pathname是相对路 ...
随机推荐
- Java相关查询记录
Version of Spring Facet could not be detected. http://yijiesuifeng.iteye.com/blog/2221444
- 一些用Css实现的效果
今天写一个笔试题,其中有一个是用css实现直角梯形的效果,我给出的答案是: <style> .wrap{ width: 100px; height: 50px; border-top:90 ...
- I/O多路复用、协程、线程、进程
select注册fd,阻塞,当有fd状态改变时返回,确认对应的fd,做下一步处理.简单来说就是先注册,注册完后休眠并设置一个定时器醒来查看,有事件就通知来取,进行后续动作,没事件就继续睡,再设闹钟.用 ...
- trunk端口配置错误导致环路
端口下 switchport mode trunk spannning-tree portfast 上述两个命令同时执行将导致环路
- jQuery index() 方法
比如同一级有多个li,获得点击的元素的下标,确定第几个. $("li").click(function(){alert($(this).index());});
- java多线程管理 concurrent包用法详解
我们都知道,在JDK1.5之前,Java中要进行业务并发时,通常需要有程序员独立完成代码实现,当然也有一些开源的框架提供了这些功能,但是这些依然没有JDK自带的功能使用起来方便.而当针对高质量 ...
- windows 下安装redis
https://github.com/MicrosoftArchive/redis/releases redis 服务安装到系统 redis-server.exe --service-install ...
- 探索未知种族之osg类生物---渲染遍历之裁剪三
前言 在osgUtil::CullVisitor,我们发现apply函数的重载中,有CullVisitor::apply(Group& node),CullVisitor::apply(Swi ...
- 探索未知种族之osg类生物---渲染遍历之器官协作
好了,现在我们经过三节的介绍我们已经大体上明确了单线程模型(SingleThreaded)下 OSG 渲染遍历的工作流程.事实上无论是场景的筛选render还是绘制cull工作,最后都要归结到场景视图 ...
- Android学习(三)
学号 20189214 <Android程序开发>第八周学习总结 教材学习内容总结 GridView GridView和ListView一样是AbsListView的子类; 都需要一个Ad ...