ostringstream, istringstream and ostream

#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int main()
{
	ostringstream ostr;
	ostr.put('h');//插入单个字符 
	ostr.put('e');
	ostr << "llo!!!";//插入字符串 
	string s1 = ostr.str();
	cout << s1 <<endl;//hello!!!
	
	ostringstream ostr_0;
	ostr_0.str("diudiu");//diudiu
	cout << ostr_0.str() << endl;
	//如果构造的时候对象内存已经存在字符串数据,
	//那么再次写数据的时候将原有数据清空覆盖

	ostr.str("world"); //world
	cout << ostr.str() << endl;
	
	int a = 100;
	float b = 3.1415;
	string s2 = "heihei";
	char c = '@';
	ostringstream ostr_1;
	ostr_1 << a << b<< c << s2;
	cout << ostr_1.str() << endl;//1003.1415@heihei 
	
	return 0;
</span><h3><span style="font-weight: normal;"><span style="font-size:14px;color:#330033;">} </span></span><span style="color:#ff0000;">
</span>
</h3>

#include <iostream>
#include <sstream>
using namespace std;
int main()
{
	istringstream ist;//istringstream是一个类。 
	ist.str("a 12.023 100");
	cout<<ist.str()<<endl;//输出:a 12.023 100 
	char a;
	float b;
	int c;
	ist>>a;
	ist>>b;
	ist>>c;
	cout<<"a = "<<a<<endl;// a = a
	cout<<"b = "<<b<<endl;// b = 12.023	
	cout<<"c = "<<c<<endl;// c = 100
	return 0;
}</span>

what the difference between ostringstream/istringstream and ostream?

Put succinctly: ostringstream provides a streambufostream requires the user to provide one.

To understand the implications, it's necessary to understand a little how streams work (and unlike some other posters, I'm not sure that there's a good explination of this on the Web). The basic abstraction of ostream is formatting textual output. You give it an int or a double (or a user defined type—more on that later), and it convert it into a stream of characters, of type char. What it does with that stream depends on the streambuf which is attached to it; this is an example of the strategy pattern, where streambuf is an abstract base class of the strategy[1]. The standard provides two implementations of streambuffilebuf and stringbuf; in practice, in all but the most trivial applications, you'll probably have some that you implement yourself.

When outputting, you always use ostream; it's the class over which the << operators are defined. You're formatting your data into a stream of characters, and you don't really care where the stream ends up.

When creating an instance: if you create an ostream, you must provide it with a streambuf yourself. More often, you'll create an ofstream or an ostringstream. These are both "convenience" classes, which derive from ostream, and provide a streambuf for it (filebuf and stringbuf, as it happens). Practically speaking, all they do is provide the necessary streambuf (which affects the constructor and the destructor, and not very much else); in the case of ofstream, there are also a few extra functions which forward to additional functions in the filebuf interface.

It's usual (but by no means required) when you define your own streambuf to provide convenience overloads of ostream (and istream, if relevant), along the same lines as ofstream or ostringstream.

By the same token, when creating an instance, it's usual to use one of the "convenience" derived classes, rather than to use ostream directly and provide your own streambuf.

And if all of this seems complicated: the iostream classes use just about all of the facilities of C++ (virtual functions, templates and function overloading all play an important role). If you're just learning C++, don't worry too much about it: just use ofstream or ostringstream when you construct an instance, but pass around references to ostream. And as you learn about techniques like virtual functions, templates and operator overloading, return to the iostreams to understand the role they play in making code more flexible.



enter image description here

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值