设为首页 加入收藏

TOP

C++任意类型转换模板
2013-04-10 11:38:52 来源: 作者: 【 】 浏览:170
Tags:任意 类型 转换 模板

  #include<iostream>

  #include<sstream>

  #include<string>

  using namespace std;

  template<class out_type,class in_value>

  out_type convert(const in_value & t)

  {

  stringstream stream;

  stream<<t;//向流中传值

  out_type result;//这里存储转换结果

  stream>>result;//向result中写入值

  return result;

  }

  int main()

  {

  string s;

  while(cin>>s)

  {

  double valdou=convert<double>(s);

  int valint=convert<int>(s);

  cout<<valdou<<endl;

  cout<<valint<<endl;

  }

  return 0;

  }

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇C++中指针小结 下一篇Vc++ 数据库编程

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: