cppannotations/annotations/yo/iostreams/ostringstream.yo
2014-12-23 21:59:45 +01:00

39 lines
2.1 KiB
Text

To hi(stream: writing to memory) write information to memory using tt(stream)
facilities, hi(ostringstream)tt(std::ostringstream) objects should be used.
As the class tt(ostringstream) is derived from the class tt(ostream) all
tt(ostream)'s facilities are available to tt(ostringstream) objects as
well. To use and define tt(ostringstream) objects the header file
tthi(sstream) must be included. In addition the class tt(ostringstream) offers
the following constructors and members:
itemization(
ithtq(ostringstream)
(ostringstream ostr(string const &init,
ios::openmode mode = ios::out))
(when specifying tt(openmode) as tt(ios::ate), the
tt(ostringstream) object is initialized by the tt(string init) and remaining
insertions are appended to the contents of the tt(ostringstream) object.
)
itt(ostringstream ostr(ios::openmode mode = ios::out)):
quote(this constructor can also be used as default
constructor. Alternatively it allows, e.g., forced additions at the end of the
information stored in the object so far (using tt(ios::app)). Example:
verb(
std::ostringstream out;
)
)
ithtq(str)(std::string str() const)
(a copy of the string that is stored inside the tt(ostringstream)
object is returned.)
itt(void str(std::string const &str)):
quote(the current object is reinitialized with new initial contents.)
)
The following example illustrates the use of the tt(ostringstream) class:
several values are inserted into the object. Then, the text contained by the
tt(ostringstream) object is stored in a tt(std::string), whose length and
contents are thereupon printed. Such tt(ostringstream) objects are most often
used for doing `type to string'hi(conversions: binary to text) conversions,
like converting tt(int) values to text. Formatting flags can be used with
tt(ostringstreams) as well, as they are part of the tt(ostream) class.
Here is an example showing an tt(ostringstream) object being used:
verbinclude(-a examples/ostringstream.cc)