Have you ever been pitted by stringstream?

Source: Internet
Author: User

Stringstream is often used to securely format several strings, numeric values to a buffer, without the need to worry about overflow, can be used to replace snprintf.
However, many users encounter problems when using stringstream because the buffer in stringstream is not properly cleared.

So what is the correct way to clear the buffer inside the stringstream class?
Stringstream ss;
The answer is: ss. str ("") method.
In addition, if you want to output the formatted string to a string through>, you must call the clear () method every time!
Therefore, during the warranty period, after each buffer is formatted, both the clear () and str ("") functions are called to reset the stingstream class.


PS1: There are some discussions on the Internet. the str ("") method does not work. str (). clear (); this may be caused by inconsistent implementation methods of the c ++ standard library. you can view the source code of the sstream file referenced by the code library.
On my linux machine, the implementations of/usr/include/c ++/4.1.0/sstream and vs2008 are consistent with those in this article.
PS2: note the difference between str () and str ("").
Str () is a copy of the internal buffer returned, str ("") is to clear the internal buffer.

Test procedure:


[Cpp]
<P> # include <sstream>
# Include <stdio. h>
Using namespace std;
Int main ()
{
Stringstream ss;
String result;
Int n = 1;
Ss. clear ();
Ss <n;
Ss> result;
Printf ("result: % s, str: % s \ n", result. c_str (), ss. str (). c_str (); </P> <P> n = 2;
Ss. clear ();
Ss <n;
Ss> result;
Printf ("result: % s, str: % s \ n", result. c_str (), ss. str (). c_str (); </P> <P> n = 3;
Ss. str ("");
Ss <n;
Ss> result;
Printf ("result: % s, str: % s \ n", result. c_str (), ss. str (). c_str (); </P> <P> n = 4;
Ss. clear ();
Ss. str ("");
Ss <n;
Ss> result;
Printf ("result: % s, str: % s \ n", result. c_str (), ss. str (). c_str ());
} </P>

# Include <sstream>
# Include <stdio. h>
Using namespace std;
Int main ()
{
Stringstream ss;
String result;
Int n = 1;
Ss. clear ();
Ss <n;
Ss> result;
Printf ("result: % s, str: % s \ n", result. c_str (), ss. str (). c_str (); n = 2;
Ss. clear ();
Ss <n;
Ss> result;
Printf ("result: % s, str: % s \ n", result. c_str (), ss. str (). c_str (); n = 3;
Ss. str ("");
Ss <n;
Ss> result;
Printf ("result: % s, str: % s \ n", result. c_str (), ss. str (). c_str (); n = 4;
Ss. clear ();
Ss. str ("");
Ss <n;
Ss> result;
Printf ("result: % s, str: % s \ n", result. c_str (), ss. str (). c_str ());
}
 

Test results:
Result: 1, str: 1
Result: 2, str: 12 // clear () is called, str ("") is not called, and the result is incorrect.
Result: 2, str: // str ("") is called, clear () is not called, and the result is incorrect.
Result: 4, str: 4 // clear () and str ("") are called, and the result is correct.

 

Enclose the internal implementation of str ("") and str:


[Cpp]
/**
* @ Brief Setting a new buffer.
* @ Param s The string to use as a new sequence.
*
* Deallocates any previous stored sequence, then copies @ a s
* Use as a new one.
*/
Void
Str (const _ string_type & _ s)
{
// Cannot use _ M_string = _ s, since v3 strings are COW.
_ M_string.assign (_ s. data (), _ s. size ());
_ M_stringbuf_init (_ M_mode );
}

/**
* @ Brief Setting a new buffer.
* @ Param s The string to use as a new sequence.
*
* Deallocates any previous stored sequence, then copies @ a s
* Use as a new one.
*/
Void
Str (const _ string_type & _ s)
{
// Cannot use _ M_string = _ s, since v3 strings are COW.
_ M_string.assign (_ s. data (), _ s. size ());
_ M_stringbuf_init (_ M_mode );
}
 

[Cpp]
// Get and set:
/**
* @ Brief Copying out the string buffer.
* @ Return A copy of one of the underlying sequences.
*
* "If the buffer is only created in input mode, the underlying
* Character sequence is equal to the input sequence; otherwise, it
* Is equal to the output sequence. "[27.7.1.2]/1
*/
_ String_type
Str () const
{
_ String_type _ ret;
If (this-> pptr ())
{
// The current egptr () may not be the actual string end.
If (this-> pptr ()> this-> egptr ())
_ Ret = _ string_type (this-> pbase (), this-> pptr ());
Else
_ Ret = _ string_type (this-> pbase (), this-> egptr ());
}
Else
_ Ret = _ M_string;
Return _ ret;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.