当前位置:编程学习 > C/C++ >>

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;
}



摘自 C小加
补充:软件开发 , C++ ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,