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

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 object
	std::ostringstream oss1;
	// using stringbuf directly
	std::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 sequence
	std::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 *this
	std::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 y
	std::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;                            // out
	std::ostringstream bar3(std::ostringstream::ate);  // out|ate
	foo3.str("Test string"); // str: sets s as the contents of the stream, discarding any previous contents
	bar3.str("Test string");
	foo3 << 101;
	bar3 << 101;
	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 0;
}

// reference: http://www.cplusplus.com/reference/sstream/istringstream/
int test_istringstream()
{
	// istringstream: Input stream class to operate on strings
	// 1. istringstream constructor
	std::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 characters
		iss6 >> val;
		std::cout << val * 2 << '\n';
	}

	// 2. rdbuf: Returns a pointer to the internal stringbuf object, with which the object was associated on construction
	std::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 sequence
	std::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 characters
		iss2 >> 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'; // 200
	bar >> val; std::cout << "bar: " << val << '\n'; // 100

	// 5. swap: Exchanges the values of the istringstream objects x and y
	std::istringstream foo2("100");
	std::istringstream bar2("200");

	swap(foo2, bar2);    // unqualified (uses argument-dependent lookup)
	int val2;
	foo2 >> val2; std::cout << "foo2: " << val2<< '\n'; // 200
	bar2 >> val2; std::cout << "bar2: " << val2 << '\n'; // 100

	return 0;
}

// reference: http://www.cplusplus.com/reference/sstream/stringstream/
int test_stringstream()
{
	// 1. stringstream: Stream class to operate on strings
	std::stringstream ss;
	ss << 100 << ' ' << 200;

	int foo, bar;
	ss >> foo >> bar;

	std::cout << "foo: " << foo << '\n'; // 100
	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[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 sequence
	std::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 *this
	std::stringstream foo4;
	std::stringstream bar4;

	foo4 << 100;
	bar4 << 200;

	foo4.swap(bar4);
	int val;
	foo4 >> val; std::cout << "foo4: " << val << '\n'; // 200
	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 << 100;
	bar5 << 200;

	std::swap(foo5, bar5);
	int val5;
	foo5 >> val5; std::cout << "foo5: " << val5 << '\n'; // 200
	bar5 >> val5; std::cout << "bar5: " << val5 << '\n'; // 100

	return 0;
}


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

 

  • 7
    点赞
  • 40
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要下载devc sstream头文件,可以按照以下步骤进行: 首先,打开任意一个浏览器,例如Google Chrome或者Firefox。 然后,在搜索引擎输入"devc sstream头文件下载",并按下Enter键。 接下来,浏览器会显示相关的搜索结果。你可以浏览搜索结果的网页,查找能够提供devc sstream头文件下载的网站或资源。 当你找到一个合适的网站时,点击进入该网站。 在该网站上,你可能需要查找相关的下载页面或者资源页面。有些网站可能会提供直接的下载链接,你只需点击该链接即可下载。 如果网站没有直接提供下载链接,你可以尝试查找一些其他选项,例如:点击"下载"按钮,或者查找类似"获取最新版本"或"下载源代码"之类的选项。 一旦你找到了适当的下载链接或选项,点击下载按钮或者按照站点上的指示进行操作。通常会弹出一个文件下载窗口,你可以选择保存文件的位置。 选择一个你可以方便找到的位置保存文件,并确认开始下载。 下载完成后,你就可以在你选择的位置找到devc sstream头文件。如果该文件是一个压缩文件,可以使用解压缩软件(如WinRAR)将其解压缩到一个你方便访问的位置。 现在,你可以在你的开发环境引用这个已经下载的devc sstream头文件,并开始使用它。 总结起来,要下载devc sstream头文件,你需要打开一个浏览器,搜索相关资源并找到一个可下载的网站,点击下载链接或者按照站点上的提示进行操作,保存文件到你方便找到的位置,最后在你的开发环境引用它。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值