暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

407.C++ string to int

学习相关知识,耗时 1 小时



Str2Int

上家公司有专门的 C++ 内部库 StringUtility,实现 string 和数字类型相互转换

  • Str2Int
  • Str2UInt
  • Str2ULongLong
  • Str2Double
  • Int2String
  • ……

我以为这些接口用起来已经很爽了,直到我在刷 LeetCode 时发现更简单的接口。

stoi

STL 本身就有提供 string 和数字类型的相互转换接口

  • stoi
  • stof
  • stod
  • stoll
  • stoull
  • to_string

    // 以下是 www.cplusplus.com 对 stoi 的说明
    int stoi (const string&  str, size_t* idx = 0int base = 10);
    int stoi (const wstring& str, size_t* idx = 0, int base = 10);


    Convert string to integer


    Parses str interpreting its content as an integral number of the specified base,
    which is returned as an int value.


    If idx is not a null pointer,
    the function also sets the value of idx to the position of the first character in str after the number.
    复制

    秉着少敲一点代码是一点的态度,之后我果断用 stoi 代替 StringUtility::Str2Int,直到一天……

    将一个非数字型 string 作为入参传给 stoi 后,产生了 core。

    atoi

    在新公司代码中发现 atoi 接口,上 www.cplusplus.com 一查,原来 STL 还有一套 string 和数字类型转换接口

    • atoi
    • atof
    • atol
    • atoll

      // 以下是 www.cplusplus.com 对 atoi 的说明
      int atoi (const char * str);


      Convert string to integer


      Parses the C-string str interpreting its content as an integral number,
      which is returned as a value of type int.


      The function first discards as many whitespace characters (as in isspaceas necessary until the first non-whitespace character is found.
      Then, starting from this character, takes an optional initial plus or minus sign followed by as many base-10 digits as possible, and interprets them as a numerical value.


      The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function.


      If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed and zero is returned.
      复制

      区别

      通过阅读 www.cplusplus.com 上对 stoi 和 atoi 的说明,能知道它们的区别:

      • stoi 的入参是 string,atoi 的入参是 c-string(char*)
      • atoi 会处理前面多余的空格
      • atoi 的入参如果不是一个数字型,会返回 0

      不过,通过自测程序,能发现 stoi 和 atoi 有更多的异同点

        string s = " 10a";
        cout << "s:" << stoi(s) << endl; // 10
        cout << "s:" << atoi(s.c_str()) << endl; // 10


        s = "";
        // cout << "s:" << stoi(s) << endl; core
        cout << "s:" << atoi(s.c_str()) << endl; // 0


        s = " ";
        // cout << "s:" << stoi(s) << endl; core
        cout << "s:" << atoi(s.c_str()) << endl; // 0


        s = " a10";
        // cout << "s:" << stoi(s) << endl; core
        cout << "s:" << atoi(s.c_str()) << endl; // 0
        复制

        总的来说,atoi 比 stoi 更安全

        • 第一个非空字符是数字,stoi 和 atoi 都能成功转换
        • 第一个非空字符不是数字,stoi 会 core 掉,atoi 返回 0

        lexical_cast

        新公司还用到了另一套 string 和数字类型的转换接口,boost 提供的接口。

          try
          {
          int i = boost::lexical_cast<int>("1");
          }
          catch (boost::bad_lexical_cast&)
          {
          continue;
          }
          复制

          最后

          通过对比,以后自己就选择使用 atoi 了,简单又安全。

          文章转载自每天一个开发小知识,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

          评论