- 最近老是用到字符串与数值之间的转换,又常常忘记用那个函数方法,现在特意记录下来下次能快速找到!!!
- 1、atof(字符串.c_str())
{
string str="123";
int num = atof(str.c_str());
cout<
- 2、stoi函数
- 将 n 进制的字符串转化为十进制
- stoi(字符串,起始位置,n进制(默认10进制)),将 n 进制的字符串转化为十进制
{
string str="123456";
int num = stoi(str);
cout<
- 3、atoi函数
- int atoi(const char *nptr)
{
char c[]="12";
int num=atoi(c);
cout<
- 4、利用ascii码转换
{
string str="1";
int num =str[0]-48;
cout<
仅适用于字符串内单个字符使用
二、数值转字符串
- to_string();
{
string str="123";
int num = atof(str.c_str());
cout< 


