在c++中char类型的数组里边装的是数字,如何转换为int类型?

有急用,谢谢大虾们了
另外我还想问问:char类型的数组里边装的是数字,又如何转换为float类型,再次谢谢了

第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;
}
相似回答