第1个回答 2007-04-15
#include <sstream>
#include <iostream>
#include <string>
using namespace std;
template <class T, class U>
T lexical_cast(U u)
{
stringstream sstrm;
sstrm << u;
T t;
sstrm >> t;
return t;
}
int main()
{
char chs[] = "12345";
int num = lexical_cast<int>(chs);
cout << num << '\n';
char fchs[] = "123.456";
float fnum = lexical_cast<float>(fchs);
cout << fnum << '\n';
string str(lexical_cast<string>(1989));
cout << str << endl;
}