Converting to std::string
suggest changestd::ostringstream
can be used to convert any streamable type to a string representation, by inserting the object into a std::ostringstream
object (with the stream insertion operator <<
) and then converting the whole std::ostringstream
to a std::string
.
For int
for instance:
#include <sstream>
int main()
{
int val = 4;
std::ostringstream str;
str << val;
std::string converted = str.str();
return 0;
}
Writing your own conversion function, the simple:
template<class T>
std::string toString(const T& x)
{
std::ostringstream ss;
ss << x;
return ss.str();
}
works but isn’t suitable for performance critical code.
User-defined classes may implement the stream insertion operator if desired:
std::ostream operator<<( std::ostream& out, const A& a )
{
// write a string representation of a to out
return out;
}
Aside from streams, since C++11 you can also use the std::to_string
(and std::to_wstring
) function which is overloaded for all fundamental types and returns the string representation of its parameter.
std::string s = to_string(0x12f3); // after this the string s contains "4851"
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents