First, a brief introduction
Ostringstream is a character set operation template class for C + +, defined in the Sstream.h header file. The Ostringstream class is typically used to perform a C-style streaming output operation, format strings, and avoid applying a large number of buffers to replace sprintf.
Derived diagram:
Second, the basic use of Ostringstream
The Ostringstream constructor form:
Explicit Ostringstream (OpenMode which = ios_base::out);
Explicit Ostringstream (const string & str, openmode which = ios_base::out);
Sometimes we need to format a string, but we don't usually know how much of a buffer is needed. In order to insure that a large buffer is often applied to prevent the buffer from being too small, the string cannot be stored entirely. At this point we can consider the use of the Ostringstream class, which can automatically allocate memory based on content, and its management of memory is quite in place. The contents of the Std::ostringstream can be obtained through the STR () and STR (string&) member functions.
III. Matters of note
STD::OSTRINGSTREAM::STR () Returns a temporary object that cannot be manipulated directly.
For example, there are the following misuse:
const char * pbuffer = OSS.STR (). C_STR ();
Note that the memory pointed to by Pbuffer has been destructor!!
Four, code testing
<span style= "FONT-SIZE:18PX;" > #include <sstream>More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/cplus/
#include <string> #include <iostream> using namespace std; void Main () {Ostringstream ostr1;//Construction Mode 1 ostringstream ostr2 ("abc");//Construction Mode 2/*------ ----------------------------------------------------------------------* * * Method STR () copies the contents of the buffer into a string object and returns the----- -----------------------------------------------------------------------* * ostr1 << "OSTR1" << (<) < Endl;
Format, where Endl will also be formatted into OSTR1 cout << ostr1.str (); /*----------------------------------------------------------------------------* * * advice: When you use the put () method, first view the present The value of the pointer to prevent misreading----------------------------------------------------------------------------/long CurPos = ostr2. TELLP ();
Returns the index position of the current insertion (that is, the value of the put pointer), starting at 0 cout << "curpos =" << curpos << Endl; OSTR2.SEEKP (2); Manually set the value of the put pointer Ostr2.put (' G '); Write ' G ' on the position of the put pointer and pointerPoint to the next character position cout << ostr2.str () << Endl; /*----------------------------------------------------------------------------* * * to reuse the same Ostringstream object, recommended: * *
* 1: Call Clear () clears the current error control state, and its prototype is void clean (iostate state=goodbit); 2: Call STR ("") to clear the buffer zero, clean the dirty data----------------------------------------------------------------------------* * ostr2.
Clear ();
Ostr2.str ("");
cout << ostr2.str () << Endl;
Ostr2.str ("_def");
cout << ostr2.str () << Endl; OSTR2 << "Gggghh";
Overwrite the original data and automatically increase the buffer cout << ostr2.str () << Endl; Ostr2.str ("");
If you do not add this sentence is a run-time error, because the _DF space is less than gggghh, resulting in reading dirty data ostr2.str ("_DF");
cout << ostr2.str () << Endl;
Output random memory value, dangerous const char* BUF = Ostr2.str (). C_STR ();
cout << buf << Endl;
Correct output _DF String ss = Ostr2.str (); const char *buffer = Ss.c_stR ();
cout << buffer << Endl; }</span>
The results of the operation are as follows:
Author: csdn Blog Lanxuezaipiao