安全地将 std::string_view 转换为 int(如 stoi 或 atoi)

新手上路,请多包涵

是否有一种 安全的标准 方法可以将 std::string_view 转换为 int


由于 C++11 std::string 让我们使用 stoi 转换为 int

   std::string str = "12345";
  int i1 = stoi(str);              // Works, have i1 = 12345
  int i2 = stoi(str.substr(1,2));  // Works, have i2 = 23

  try {
    int i3 = stoi(std::string("abc"));
  }
  catch(const std::exception& e) {
    std::cout << e.what() << std::endl;  // Correctly throws 'invalid stoi argument'
  }

但是 stoi 不支持 std::string_view 。因此,或者,我们可以使用 atoi ,但必须非常小心,例如:

   std::string_view sv = "12345";
  int i1 = atoi(sv.data());              // Works, have i1 = 12345
  int i2 = atoi(sv.substr(1,2).data());  // Works, but wrong, have i2 = 2345, not 23

所以 atoi 也不起作用,因为它基于空终止符 '\0' (例如 sv.substr 不能简单地插入/添加一个)。

现在,由于 C++17 还有 from_chars ,但是在提供不良输入时似乎不会抛出:

   try {
    int i3;
    std::string_view sv = "abc";
    std::from_chars(sv.data(), sv.data() + sv.size(), i3);
  }
  catch (const std::exception& e) {
    std::cout << e.what() << std::endl;  // Does not get called
  }

原文由 Phil-ZXX 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 1.6k
1 个回答

std::from_chars 函数不会抛出,它只返回一个类型为 from_chars_result 的值,这是一个具有两个字段的结构:

 struct from_chars_result {
    const char* ptr;
    std::errc ec;
};

当函数返回时,您应该检查 ptrec 的值:

 #include <iostream>
#include <string>
#include <charconv>

int main()
{
    int i3;
    std::string_view sv = "abc";
    auto result = std::from_chars(sv.data(), sv.data() + sv.size(), i3);
    if (result.ec == std::errc::invalid_argument) {
        std::cout << "Could not convert.";
    }
}

原文由 Ron 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
logo
Stack Overflow 翻译
子站问答
访问
宣传栏