std::stringstream::str() is the method you are looking for.
With std::stringstream :
template <class T> std::string YourClass::NumericToString(const T & NumericValue) { std::stringstream ss; ss << NumericValue; return ss.str(); }
std::stringstream is a more general tool. You can use the more specialized class std::ostringstream for this specific task.
template <class T> std::string YourClass::NumericToString(const T & NumericValue) { std::ostringstream oss; oss << NumericValue; return oss.str(); }
If you are working with the std::wstring string type, you should instead choose std::wstringstream or std::wostringstream .
template <class T> std::wstring YourClass::NumericToString(const T & NumericValue) { std::wostringstream woss; woss << NumericValue; return woss.str(); }
if you want the character type of your string to be selected at run time, you must also make it a template variable.
template <class CharType, class NumType> std::basic_string<CharType> YourClass::NumericToString(const NumType & NumericValue) { std::basic_ostringstream<CharType> oss; oss << NumericValue; return oss.str(); }
For all of the above methods, you must include the following two header files.
#include <string> #include <sstream>
Note that the NumericValue argument in the above examples can also be passed as std::string or std::wstring for use with instances of std::ostringstream and std::wostringstream respectively. NumericValue not required to be a numeric value.
hkBattousai Sep 26 '15 at 20:53 on 2015-09-26 20:53
source share