C++任意类型转换模板
3549 点击·0 回帖
![]() | ![]() | |
![]() | #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; } | |
![]() | ![]() |