【备忘录】c++ 整形浮点<=>字符串 类型转换

阅读 112

2023-04-13


一、C风格字符串


1.<stdlib.h>中的转换函数 
atoi atol atoll
itoa ltoa ultoa lltoa
atof ecvt fcvt gcvt
strtol strtoul strtoll strtod


2.
sprintf  sscanf (功能更强大)


二、std::string


1.标准库转换函数 
(using namespace std;)
to_string
stoi stol stoul stoll stoul
stof stod stold


2.字符串流

#include<sstream>
using namespace std;
int ValueOf(const string &str) 
{
    stringstream ss(str);
    int retn;
    ss>>retn;
    if(!ss) throw exception("转换错误!");
    return retn;
}


精彩评论(0)

0 0 举报