简体   繁体   English

使用stringstream操作基础字符串对象

[英]Manipulating Underlying string object using stringstream

I am new to C++ and still juggling with stringstream. 我是C ++的新手,但仍在使用stringstream。 I have written a small piece of code which doesn't give required output The code is as follows: 我写了一小段不提供所需输出的代码,代码如下:

#include "iostream"
#include "sstream"

using namespace std;
int main ()
{
   string xyz;
   cout << "Initial xyz : " << xyz << endl;
   stringstream s1 ( xyz );
   s1 << "Hello";
   cout << "Final xyz : " << xyz << endl;
}

Output: 输出:

Initial xyz : 
Final xyz : 

My understanding is stringstream works as a wrapper around a string object.Therefore once stringstream has been initialized with a string object, any write operation on the stream will affect the underlying string object.So when I write "Hello" to stream and print the string xyz, it should display "Hello". 我的理解是stringstream充当字符串对象的包装器。因此,一旦用字符串对象初始化了stringstream,对流的任何写操作都会影响底层的字符串对象。因此,当我编写“ Hello”以流式传输并打印字符串时xyz,它应该显示“ Hello”。 But this clearly does not seem to be the case. 但这显然不是事实。 Can someone please tell me where am I wrong and how can I manipulate the underlying string using stringstream? 有人可以告诉我哪里错了,如何使用stringstream操作底层字符串? Thanks in advance ! 提前致谢 ! Vimal VIMAL

It is not wrapper. 它不是包装器。 It allocates own string object inside. 它在内部分配自己的字符串对象。 But you can assign your xyz: 但是您可以分配您的xyz:

s1 << "Hello";
xyz = s1.str();

Stringstream is a wrapper around a string object - it's own internal string object. Stringstream是一个字符串对象的包装器-它是自己的内部字符串对象。 You cannot set it to wrap an external string, as that would be incredibly unsafe. 您不能将其设置为包装外部字符串,因为这绝对是不安全的。

It's not a wrapper for a string, it's a stream . 它不是字符串的包装器,而是 ( just like you can talk about an audio stream) (就像您可以谈论音频流一样)

It allows you to manipulate a virtual string almost the same way you manipulate a file. 它使您几乎可以像处理文件一样来操作虚拟字符串。 By adding data sequencially, or reading data sequentially. 通过顺序添加数据或顺序读取数据。

Here is the cplusplus reference for stringstream 这是stringstream的cplusplus参考

And when you want to use the constructed string, you call str() on it. 当您要使用构造的字符串时,可以在其上调用str()。

By the way, one of the common use of stringstream is to use it as a string converter. 顺便说一句,stringstream的常见用途之一是将其用作字符串转换器。 It should be prefered to all atoi itoa stuffs. 它应该是所有atoi itoa东西的首选。

Thanks a lot for your prompt replies. 非常感谢您的及时答复。 My understanding now is : -> If a string needs to be treated as a stream ( example for ease of extraction ), then create a new stringstream object and initialize it with the string.The stream will copy the contents of the string into its own internal string.Any subsequent write operation on the stream will not affect the original string. 我现在的理解是:->如果需要将字符串视为流(例如,为了易于提取),则创建一个新的stringstream对象并使用该字符串对其进行初始化。该流会将字符串的内容复制到其自身中内部字符串。对流的任何后续写入操作都不会影响原始字符串。

-> However if an entirely new string need to be created, just create a blank stringstream object without initializing it with a string object.Write to the stream and once done, just use the str() method of stringstream to copy the contents of stream's string to your own string. ->但是,如果需要创建一个全新的字符串,只需创建一个空白的stringstream对象而不用字符串对象对其进行初始化即可。将其写入流并完成后,只需使用stringstream的str()方法复制流的内容即可字符串到您自己的字符串。 I tried this as follows and it is working fine. 我尝试如下,它工作正常。

#include "iostream"
#include "sstream"

using namespace std;
int main ()
{
   stringstream s1;
   s1 << "Hello";
   string xyz = s1.str();
   cout << "Final xyz : " << xyz << endl;
}

In any case, my original query has been satisfactorily solved. 无论如何,我的原始查询已得到令人满意的解决。 Thanks once again. 再次感谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM