新网创想网站建设,新征程启航

为企业提供网站建设、域名注册、服务器等服务

如何通过stringstream实现常用的类型转换

这篇文章主要讲解了如何通过stringstream实现常用的类型转换,内容清晰明了,对此有兴趣的小伙伴可以学习一下,相信大家阅读完之后会有帮助。

创新互联公司专注为客户提供全方位的互联网综合服务,包含不限于成都网站设计、做网站、成都外贸网站建设公司、特克斯网络推广、小程序设计、特克斯网络营销、特克斯企业策划、特克斯品牌公关、搜索引擎seo、人物专访、企业宣传片、企业代运营等,从售前售中售后,我们都将竭诚为您服务,您的肯定,是我们最大的嘉奖;创新互联公司为所有大学生创业者提供特克斯建站搭建服务,24小时服务热线:18982081108,官方网址:www.cdcxhl.com

其他类型转成string

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

string转成其他类型

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

类型之间的相互转换

template 
void toConvert(const inputType &input, outputType &output){

  stringstream ss;
  ss.clear();
  ss << input;
  ss >> output;
}

完整代码

#include 
#include 
#include 
using namespace std;

template 
void toString(string& result,const T& t);
template 
void stringToOther(T &t, const string &s);
template 
void toConvert(const inputType &input, outputType &output);

int main(int argc, char** argv)
{
  string s1;
  double a =1.1111;
  toString(s1,a);
  cout<
void toString(string& result,const T &t)
{
  //将各种数值转换成字符串
  ostringstream oss;
  oss.clear();
  oss << t;
  result.clear();
  result = oss.str();
}

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

template 
void toConvert(const inputType &input, outputType &output){

  stringstream ss;
  ss.clear();
  ss << input;
  ss >> output;
}

看完上述内容,是不是对如何通过stringstream实现常用的类型转换有进一步的了解,如果还想学习更多内容,欢迎关注创新互联行业资讯频道。


新闻名称:如何通过stringstream实现常用的类型转换
地址分享:http://www.wjwzjz.com/article/gcssdi.html
在线咨询
服务热线
服务热线:028-86922220
TOP