标签:oid const tty std int put other type 完整
template <class T>
void toString(string& result,const T &t)
{
//将各种数值转换成字符串
ostringstream oss;
oss.clear();
oss << t;
result.clear();
result = oss.str();
}
template <class T>
void stringToOther(T &t, const string &s)
{
stringstream ss;
ss.clear();
ss << s;
ss >> t;
}
template <class inputType,class outputType>
void toConvert(const inputType &input, outputType &output){
stringstream ss;
ss.clear();
ss << input;
ss >> output;
}
#include <sstream>
#include <iostream>
#include <string>
using namespace std;
template <class T>
void toString(string& result,const T& t);
template <class T>
void stringToOther(T &t, const string &s);
template <class inputType,class outputType>
void toConvert(const inputType &input, outputType &output);
int main(int argc, char** argv)
{
string s1;
double a =1.1111;
toString(s1,a);
cout<<s1<<endl;
double b = 0;
double &bptr =b;
stringToOther(bptr,s1);
cout<<bptr<<endl;
string s2 ="2.222";
double c1 =0;
double &c1ptr = c1;
toConvert(s2,c1ptr);
cout<<c1ptr<<endl;
return 0;
}
template <class T>
void toString(string& result,const T &t)
{
//将各种数值转换成字符串
ostringstream oss;
oss.clear();
oss << t;
result.clear();
result = oss.str();
}
template <class T>
void stringToOther(T &t, const string &s)
{
stringstream ss;
ss.clear();
ss << s;
ss >> t;
}
template <class inputType,class outputType>
void toConvert(const inputType &input, outputType &output){
stringstream ss;
ss.clear();
ss << input;
ss >> output;
}
标签:oid const tty std int put other type 完整
原文地址:https://www.cnblogs.com/cyssmile/p/12790946.html