C++:如何优雅的转换数字

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 解析:

https://en.cppreference.com/w/cpp/string/byte/strtoul

https://cplusplus.com/reference/cstdlib/strtoul/?kw=strtoul

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值