C++/C++11中头文件<sstream>介绍

来源:互联网 发布:电脑语音录入软件 编辑:程序博客网 时间:2024/06/02 03:16

C++使用标准库类来处理面向流的输入和输出:(1)、iostream处理控制台IO;(2)、fstream处理命名文件IO;(3)、stringstream完成内存string的IO。

类fstream和stringstream都是继承在类iostream的。输入类都继承自istream,输出类都继承自ostream。因此,可以在istream对象上执行的操作,也可在ifstream或istringstream对象上执行。继承自ostream的输出类也有类似情况。

关于iostream的介绍可以参考: http://blog.csdn.net/fengbingchun/article/details/63685373

关于fstream的介绍可以参考: http://blog.csdn.net/fengbingchun/article/details/51570728

string流:sstream头文件定义了三个类型来支持内存IO,这些类型可以向string写入数据,从string读取数据,就像string是一个IO流一样。

istringstream从string读取数据,ostringstream向string写入数据,而头文件stringstream既可从string读数据也可向string写数据。与fstream类型类似,头文件sstream中定义的类型都继承iostream头文件中定义的类型。除了继承得来的操作,sstream中定义的类型还增加了一些成员来管理与流相关联的string.

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

C++ IO heads, templates and class ( https://www.ntu.edu.sg/home/ehchua/programming/cpp/cp10_IO.html ):


下面是从其他文章中copy的测试代码,详细内容介绍可以参考对应的reference:

#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 objectstd::ostringstream oss1;// using stringbuf directlystd::stringbuf *pbuf = oss1.rdbuf();pbuf->sputn("Sample string", 13);std::cout << pbuf->str() << std::endl;// 2. 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 sequencestd::ostringstream oss2;oss2 << "One hundred and one: " << 101;std::string s1 = oss2.str();std::cout << s1 << '\n';// 3. swap: c++11, Exchanges all internal data between x and *thisstd::ostringstream foo;std::ostringstream bar;foo << 100;bar << 200;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 ystd::ostringstream foo2;std::ostringstream bar2;foo2 << 100;bar2 << 200;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;                            // outstd::ostringstream bar3(std::ostringstream::ate);  // out|atefoo3.str("Test string"); // str: sets s as the contents of the stream, discarding any previous contentsbar3.str("Test string");foo3 << 101;bar3 << 101;std::cout << foo3.str() << '\n'; // 101t stringstd::cout << bar3.str() << '\n'; // Test string101std::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_stringdouble d = 123.45;std::ostringstream os3;os3 << d;std::string s3 = "The value of d is " + os3.str();std::cout << s3 << std::endl;return 0;}// reference: http://www.cplusplus.com/reference/sstream/istringstream/int test_istringstream(){// istringstream: Input stream class to operate on strings// 1. istringstream constructorstd::istringstream is("the quick brown fox");std::string s;while (is >> s) {std::cout << s << std::endl;}std::string stringvalues = "125 320 512 750 333";std::istringstream iss6(stringvalues);for (int n = 0; n<5; n++) {int val;// Elements in a character stream are considered to be separated by 'white space'// which is basically space, tab and newline charactersiss6 >> val;std::cout << val * 2 << '\n';}// 2. rdbuf: Returns a pointer to the internal stringbuf object, with which the object was associated on constructionstd::istringstream iss;std::stringbuf *pbuf = iss.rdbuf();// using stringbuf directly:pbuf->str("Example string");int size = pbuf->in_avail();while (pbuf->in_avail()>0)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 sequencestd::istringstream iss2;std::string strvalues = "32 240 2 1450";iss2.str(strvalues);for (int n = 0; n<4; n++) {int val;// Elements in a character stream are considered to be separated by 'white space'// which is basically space, tab and newline charactersiss2 >> val;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("100");std::istringstream bar("200");foo.swap(bar);int val;foo >> val; std::cout << "foo: " << val << '\n'; // 200bar >> val; std::cout << "bar: " << val << '\n'; // 100// 5. swap: Exchanges the values of the istringstream objects x and ystd::istringstream foo2("100");std::istringstream bar2("200");swap(foo2, bar2);    // unqualified (uses argument-dependent lookup)int val2;foo2 >> val2; std::cout << "foo2: " << val2<< '\n'; // 200bar2 >> val2; std::cout << "bar2: " << val2 << '\n'; // 100return 0;}// reference: http://www.cplusplus.com/reference/sstream/stringstream/int test_stringstream(){// 1. stringstream: Stream class to operate on stringsstd::stringstream ss;ss << 100 << ' ' << 200;int foo, bar;ss >> foo >> bar;std::cout << "foo: " << foo << '\n'; // 100std::cout << "bar: " << bar << '\n'; // 200// 2. rdbuf: Returns a pointer to the internal stringbuf object, with which the object was associated on constructionstd::stringstream ss2;// using stringbuf directly:std::stringbuf *pbuf = ss2.rdbuf();pbuf->sputn("Example string", 13);char buffer[80];pbuf->sgetn(buffer, 80);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 sequencestd::stringstream ss3;ss3.str("Example string");std::string s3 = ss3.str();std::cout << s3 << '\n';// 4.1 swap: c++11, Exchanges all internal data between x and *thisstd::stringstream foo4;std::stringstream bar4;foo4 << 100;bar4 << 200;foo4.swap(bar4);int val;foo4 >> val; std::cout << "foo4: " << val << '\n'; // 200bar4 >> val; std::cout << "bar4: " << val << '\n'; // 100// 4.2 swap(stringstream): Exchanges the values of the stringstream objects x and ystd::stringstream foo5;std::stringstream bar5;foo5 << 100;bar5 << 200;std::swap(foo5, bar5);int val5;foo5 >> val5; std::cout << "foo5: " << val5 << '\n'; // 200bar5 >> val5; std::cout << "bar5: " << val5 << '\n'; // 100return 0;}

GitHub:https://github.com/fengbingchun/Messy_Test

0 0
原创粉丝点击