One: StringStream clear vs. STR ("")
Because the OJ platform needs to produce. In. Out such test data, if the occasional production to nothing, can be a large number of problems, so I want to automatically generate these files through FStream, and keep the file name continues to grow.
Like 1.in 2.in 3.in ... This involves the conversion of the int type to the string type, and I learned a way through the unfriendly mother's search.
converts int and string by using the StringStream class.
StringStream's header file is Sstream
I found that the duplicate writes will appear in front of the things that remain in the stream.
At this point, the first thing I think about is the clear () function, which is used to empty the stream.
But when a value pass operation is performed by String = Stream.str (), the old data still exists,
When passing stream>>string, the old data does not exist.
String test;
String test2;
StringStream SS;
for (int j=0;j<10;j++)
{
Ss.clear ();
Ss.str ("");
ss<<j;
Test = Ss.str ();
ss>>test2;
cout <<test<< "<<ss.str () <<" "<<test2<<endl;
}
StringStream is commonly used to safely format several strings, values into a buffer, without worrying about overflow, which can be used to replace snprintf. But many people encounter problems when using StringStream because the buffer inside the stringstream is not properly emptied.
So what is the correct way to clear the buffer inside the StringStream class? StringStream SS; The answer is: Ss.str ("")
Method. In addition, you must call the clear () method every time you need to output the formatted string through >> to a string! So, during the insurance period, each time the buffer is formatted,
All through clear (), str ("") two functions are called, the Stingstream class is reset.
So, when the object of the Streamstring class is reused, call Clear (), the Str (""), and the Stingstream class is reset, and then the data is converted by using the Streamstring class.
PS1: There are some discussions on the web, saying that the Ss.str ("") method is used regardless of the ss.str (). Clear (); This may be caused by inconsistencies in the implementation of the C + + standard library. The source code of the Sstream file referenced by the codebase can be viewed by itself.
On my Linux machine,/usr/include/c++/4.1.0/sstream, and the implementation of VS2008, are consistent with this article.
PS2: Note the difference between STR () and STR ("")
STR () is a copy that returns an internal buffer, and STR ("") empties the internal buffer.
Finally, the implementation of the source code within STR and STR () is attached.
/**
* @brief Setting a new buffer.
* @param s The string to use as a new sequence.
*
* deallocates any previous stored sequence, then copies @a s to
* Use as a new one.
*/
void Str (const __string_type& __s)
{
Cannot use _m_string = __s, since v3 strings is COW.
_m_string.assign (__s.data (), __s.size ());
_m_stringbuf_init (_m_mode);
}
Get and set:
/**
* @brief Copying out the string buffer.
* @return A copy of the underlying sequences.
*
* "If the buffer is a 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 () is 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;
}
C + + string learning (in update)