Converting numbers to strings using streams

Stream buffer classes are responsible for obtaining characters and writing characters from the appropriate source (file, console, and so on) and are derived from the abstract class basic_streambuf from <streambuf>. This base class defines two virtual methods, overflow and underflow, which are overridden by the derived classes to write and read characters (respectively) to and from the device associated with the derived class. The stream buffer class does the basic action of getting or putting items into a stream, and since the buffer handles characters, the class is templated with parameters for the character type and character traits.

As the name suggests, if you use a basic_stringbuf the stream buffer will be a string, so the source for read characters and the destination for written characters is that string. If you use this class to provide the buffer for a stream object, it means that you can use the insertion or extraction operators written for streams to write or read formatted data into or out of a string. The basic_stringbuf buffer is extendable, so as you insert items in the stream, the buffer will extend appropriately. There are typedef, where the buffer is a string (stringbuf) or a wstring (wstringbuf).

For example, imagine you have a class that you have defined and you have also defined an insertion operator so that you can use this with the cout object to print the value to the console:

    struct point 
{
double x = 0.0, y = 0.0;
point(){}
point(double _x, double _y) : x(_x), y(_y) {}
};


ostream& operator<<(ostream& out, const point& p)
{
out << "(" << p.x << "," << p.y << ")";
return out;
}

Using this with the cout object is simple--consider the following piece of code:

    point p(10.0, -5.0); 
cout << p << "n"; // (10,-5)

You can use the stringbuf to direct the formatted output to a string rather than the console:

    stringbuf buffer;  
ostream out(&buffer);
out << p;
string str = buffer.str(); // contains (10,-5)

Since the stream object handles the formatting it means that you can insert any data type for which there is an insertion operator, and you can use any of the ostream formatting methods and any of the manipulators. The formatted output from all of these methods and manipulators will be inserted into the string object in the buffer.

Another option is to use the basic_ostringstream class in <sstream>. This class is templated on the character type of the strings used as the buffer (so the string version is ostringstream). It is derived from the ostream class, so you can use instances wherever you would use an ostream object. The formatted results can be accessed through the str method:

    ostringstream os; 
os << hex;
os << 42;
cout << "The value is: " << os.str() << "n";

This code obtains the value of 42 in hexadecimal (2a); this is achieved by inserting the hex manipulator in the stream and then inserting the integer. The formatted string is obtained by calling the str method.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset