用类模板实现任何类型之间转换

来源:互联网 发布:foreach遍历多维数组 编辑:程序博客网 时间:2024/06/08 05:03
#include <bits/stdc++.h>
using namespace std;
template <class out_type,class in_value>
out_type convert(const in_value &t)
{
    out_type ans;
    stringstream a;
    a<<t;
    a>>ans;
    return ans;
}
int main()
{
    string b;
    for(int i=0; i<10; i++)
    {
        b=convert<string>(i);
        cout<<b<<endl;
    }
    return 0;
}
0 0