如何用stringstream实现常见类型转换的示例代码?

2026-04-29 12:152阅读0评论SEO资源
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计381个文字,预计阅读时间需要2分钟。

如何用stringstream实现常见类型转换的示例代码?

cppstring toString(const T &t){ stringstream oss; oss <

其他类型转成string

template <class T> void toString(string& result,const T &t) { //将各种数值转换成字符串 ostringstream oss; oss.clear(); oss << t; result.clear(); result = oss.str(); }

string转成其他类型

template <class T> void stringToOther(T &t, const string &s) { stringstream ss; ss.clear(); ss << s; ss >> t; }

类型之间的相互转换

template <class inputType,class outputType> void toConvert(const inputType &input, outputType &output){ stringstream ss; ss.clear(); ss << input; ss >> output; }

完整代码

#include <sstream> #include <iostream> #include <string> using namespace std; template <class T> void toString(string& result,const T& t); template <class T> void stringToOther(T &t, const string &s); template <class inputType,class outputType> void toConvert(const inputType &input, outputType &output); int main(int argc, char** argv) { string s1; double a =1.1111; toString(s1,a); cout<<s1<<endl; double b = 0; double &bptr =b; stringToOther(bptr,s1); cout<<bptr<<endl; string s2 ="2.222"; double c1 =0; double &c1ptr = c1; toConvert(s2,c1ptr); cout<<c1ptr<<endl; return 0; } template <class T> void toString(string& result,const T &t) { //将各种数值转换成字符串 ostringstream oss; oss.clear(); oss << t; result.clear(); result = oss.str(); } template <class T> void stringToOther(T &t, const string &s) { stringstream ss; ss.clear(); ss << s; ss >> t; } template <class inputType,class outputType> void toConvert(const inputType &input, outputType &output){ stringstream ss; ss.clear(); ss << input; ss >> output; }

到此这篇关于通过stringstream实现常用的类型转换实例代码的文章就介绍到这了,更多相关stringstream实现常用的类型转换内容请搜索自由互联以前的文章或继续浏览下面的相关文章希望大家以后多多支持自由互联!

如何用stringstream实现常见类型转换的示例代码?
标签:类型

本文共计381个文字,预计阅读时间需要2分钟。

如何用stringstream实现常见类型转换的示例代码?

cppstring toString(const T &t){ stringstream oss; oss <

其他类型转成string

template <class T> void toString(string& result,const T &t) { //将各种数值转换成字符串 ostringstream oss; oss.clear(); oss << t; result.clear(); result = oss.str(); }

string转成其他类型

template <class T> void stringToOther(T &t, const string &s) { stringstream ss; ss.clear(); ss << s; ss >> t; }

类型之间的相互转换

template <class inputType,class outputType> void toConvert(const inputType &input, outputType &output){ stringstream ss; ss.clear(); ss << input; ss >> output; }

完整代码

#include <sstream> #include <iostream> #include <string> using namespace std; template <class T> void toString(string& result,const T& t); template <class T> void stringToOther(T &t, const string &s); template <class inputType,class outputType> void toConvert(const inputType &input, outputType &output); int main(int argc, char** argv) { string s1; double a =1.1111; toString(s1,a); cout<<s1<<endl; double b = 0; double &bptr =b; stringToOther(bptr,s1); cout<<bptr<<endl; string s2 ="2.222"; double c1 =0; double &c1ptr = c1; toConvert(s2,c1ptr); cout<<c1ptr<<endl; return 0; } template <class T> void toString(string& result,const T &t) { //将各种数值转换成字符串 ostringstream oss; oss.clear(); oss << t; result.clear(); result = oss.str(); } template <class T> void stringToOther(T &t, const string &s) { stringstream ss; ss.clear(); ss << s; ss >> t; } template <class inputType,class outputType> void toConvert(const inputType &input, outputType &output){ stringstream ss; ss.clear(); ss << input; ss >> output; }

到此这篇关于通过stringstream实现常用的类型转换实例代码的文章就介绍到这了,更多相关stringstream实现常用的类型转换内容请搜索自由互联以前的文章或继续浏览下面的相关文章希望大家以后多多支持自由互联!

如何用stringstream实现常见类型转换的示例代码?
标签:类型