欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

【提醒】C++中整数、浮点数和字符串类型之间的转换

最编程 2024-01-12 21:57:02
...

一、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;
} 

推荐阅读