C小加

厚德 博学 求真 至善 The bright moon and breeze
posts - 145, comments - 195, trackbacks - 0, articles - 0
  C++博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

C++任意类型转换模板

Posted on 2012-04-12 15:13 C小加 阅读(2086) 评论(5)  编辑 收藏 引用 所属分类: C++和面向对象模板
#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;
}

Feedback

# re: C++任意类型转换模板  回复  更多评论   

2012-04-12 15:47 by SunRise_at
老公。。。。。。。。。。。。。。。。

# re: C++任意类型转换模板  回复  更多评论   

2012-04-12 21:47 by WindyWinter
把-1转换成1?

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理