1. 反例:
void function1(const string str)
{
int num = atoi(str.c_str());
// 代码块
}
C标准库中的atoi, atol, atoll, atof函数,在字符串转换的结果无法表示成相应的数值时,会导致程序产生未定义行为,并且未提供足够的出错信息。
2. 使用异常
c++11开始提供了stoi, stol, stoll,stof, stod, stold等函数,使用这些函数时需要处理无法正确转换的异常。
void function2(const string str)
{
try {
int num = stoi(str.c_str());
cout << num << endl;
} catch (const invalid_argument& e) {
// 错误处理
} catch (const out_of_range& e) {
// 错误处理
}
}
3. 禁用异常场景
在禁用异常的场景中,可以使用C标准库中的strtol, strtoll, strtof, strtod, strtold等函数或者使用C++的stoi, stol, stoll,stof, stod, stold等函数,并处理未成功转换的错误。
bool function3(const string &str, unsigned int &num)
{
errno = 0; // 调用strtoul之前应该将errno赋值为0
char *endPtr = nullptr;
unsigned int result = (unsigned int)strtoul(str.c_str(), &endPtr, 10);
if (errno == ERANGE || endPtr == str.c_str() || *endPtr != '\0') {
cerr << "Failed to convert the string " << str << " to a digit." << endl;
return false;
}
num = result;
return true;
}
int main()
{
unsigned int num = 0;
string inputStr1 = "123";
if (function3(inputStr1, num)) {
printf("convert string[%s] to [%u] \n", inputStr1.c_str(), num);
}
string inputStr2 = "123a";
if (function3(inputStr2, num)) {
printf("convert string[%s] to [%u]\n", inputStr2.c_str(), num);
}
string inputStr3 = " 1234";
if (function3(inputStr3, num)) {
printf("convert string[%s] to [%u]\n", inputStr3.c_str(), num);
}
}
4. 发现
在禁用异常的场景中,我们可以发现前面的空格被自动忽略了。经查阅资料发现 strtoul 会自动屏蔽字符串前面的空格。官方文档
Discards any whitespace characters (as identified by calling isspace) until the first non-whitespace character is found.
5. 遇到问题可以求助比较官方的网站
strtoul 解析: