Converting a Number to a String

The standard strstream family of classes enables you to convert numeric values into their string representation easily. You need to instantiate an strstream object, insert the numeric value to it using the overloaded << operator, and then extract the string from the strstream object using the str() member function. The following program converts a float variable to a string object using strstream:

 #include #include using namespace std;int main(){ ostrstream s; float f = 6.6777; s<

Note that strstream overload to << operator for all built-in types, e.g., int, float, double, char, pointers, etc. In the following example, a strstream object stores the value of a void pointer and then prints it on the screen:

 int main(){ ostrstream s; void *p = &s; // take address of s s<
Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

Recent Articles:

©2023 Copyright DevX - All Rights Reserved. Registration or use of this site constitutes acceptance of our Terms of Service and Privacy Policy.

Sitemap