开发者

深入解析 C++中std::stoul 函数

目录
  • 1. std::stoul 的基本介绍
    • 1.1 std::stoul 的函数原型
    • 1.2 参数解析
    • 1.3 返回值
  • 2. std::stoul 的实现原理
    • 2.1 解析实现代码
  • 3. std::stoul 的实际用法
    • 3.1 基础示例
    • 3.2 使用 pos 参数记录转换结束的位置
    • 3.3 处理不同进制的转换
  • 4. 处理异常情况
    • 4.1 输入不是有效的数字
    • 4.2 数值超出 unsigned long 范围
  • 5. std::stoul 相关函数
    • 6. 结论

      std::stoul(String to Unsigned Long)是 C++ 标准库中的一个字符串转换函数,它用于将 std::string 或 std::wstring 转换为 unsigned long 类型的整数,并支持不同进制的转换(如二进制、十进制、十六进制等)。

      1. std::stoul 的基本介绍

      std::stoul 定义在 <string> 头文件中,并位于 std 命名空间下。它的主编程客栈要功能是将字符串转换为 unsigned long 类型的整数,并且支持不同进制的转换

      1.1 std::stoul 的函数原型

      unsigned long stoul(const std::string& str, size_t* pos = nullandroidptr, int base = 10);
      unsigned long stoul(const std::wstring& str, size_t* pos = nullptr, int base = 10);
      

      1.2 参数解析

      • str:要转换的字符串,必须是有效的无符号整数格式。
      • pos(可选):如果提供 posstoul 会在 *pos 中存储转换结束的位置(即最后一个转换的字符的下一个位置)。
      • base(可选):表示字符串所使用的进制,默认值为 10(十进制),支持 2 到 36 进制。

      1.3 返回值

      • 成功:返回转换后的 unsigned long 类型整数。
      • 失败:如果字符串不是有效的数字或超出了 unsigned long 类型的范围,会抛出异常。

      2. std::stoul 的实现原理

      在 GCC 标准库的 <string> 头文件中,std::stoul 的底层实现基于 std::strtoul,如下:

      inline unsigned long
      stoul(const string&amp; __str, size_t* __idx = 0, int __base = 10)
      {
          return __gnu_cxx::__stoa(&amp;std::strtoul, "stoul", __str.c_str(),
                                   __idx, __base);
      }
      

      2.1 解析实现代码

      • 调用 std::strtoulstd::strtoul(String to Unsigned Long)是 C 语言中的标准库函数,它可以将 C 风格的字符串转换为 unsigned long 类型的整数,同时支持进制转换。
      • 使用 __gnu_cxx::__stoa 进行封装__stoa 是 GNU C++ 扩展库中的一个工具函数,它用于将 unsigned long 转换为 unsigned long,并进行错误检查。
      • __str.c_str()std::string 的 .c_str() 方法返回一个 const char*,将 std::string 转换为 C 风格字符串,以便 std::strtoul 解析。

      3. std::stoul 的实际用法

      3.1 基础示例

      #include <IOStream>
      #include <string>
      
      int main() {
          std::string str = "123456789";
          unsigned long num = std::stoul(str);  // 默认10进制
          std::cout << "转换结果: " << num << std::endl;
          return 0;
      }
      

      输出:

      转换结果: 123456789

      3.2 使用 pos 参数记录转换结束的位置

      #include <iostream>
      #include <string>
      
      int main() {
          std::string str = "12345xyz";
          std::size_t pos;
          unsigned long num = std::stoul(str, &pos);
          
          std::cout << "转换结果: " << num << std::endl;
          std::cout << "转换结束位置: " << pos << std::endl;
          
          return 0;
      }
      

      输出:

      转换结果: 12345

      转换结束位置: 5

      解析:

      • stoul 解析 "12345" 后遇到 "xyz",停止转换,并将 pos 设为 5

      3.3 处理不同进制的转换

      #include <python;iostream>
      #include <string>
      
      int main() {
          std::string str = "1A";  // 16进制
          unsigned long num = std::stoul(str, nullptr, 16);  // 以16进制解析
          std::cout << "转换结果: " << num << std::endl;
          
          return 0;
      }
      

      输出:

      转换结果: 26

      解析:

      • 1A 在十六进制中等于 26,所以 stoul 返回 26

      其他进制示例

      std::stoul("1010", nullptr, 2);编程  // 以二进制解析,返回 10
      std::stoul("75", nullptr, 8);    // 以八进制解析,返回 61
      std::stoul("1F", nullptr, 16);   // 以十六进制解析,返回 31
      

      4. 处理异常情况

      4.1 输入不是有效的数字

      如果 str 不是有效的整数格式,std::stoul 会抛出 std::invalid_argument 异常:

      #include <iostream>
      #include <string>
      #include <stdexcept>
      
      int main() {
          try {
              std::string str = "abc";  
              unsigned long num = std::stoul(str);
              std::cout << "转换结果: " << num << std::endl;
          } catch (const std::invalid_argument& e) {
              std::cerr << "错误: 无效的数字字符串 -> " << e.what() << std::endl;
          }
      
          return 0;
      }js
      

      输出:

      错误: 无效的数字字符串 -> stoul

      4.2 数值超出 unsigned long 范围

      如果字符串表示的数值超出 unsigned long 的范围,std::stoul 会抛出 std::out_of_range 异常:

      #include <iostream>
      #include <string>
      #include <climits>
      
      int main() {
          try {
              std::string str = "99999999999999999999";  
              unsigned long num = std::stoul(str);
              std::cout << "转换结果: " << num << std::endl;
          } catch (const std::out_of_range& e) {
              std::cerr << "错误: 数值超出范围 -> " << e.what() << std::endl;
          }
      
          return 0;
      }
      

      输出:

      错误: 数值超出范围 -> stoul

      5. std::stoul 相关函数

      函数作用返回类型
      std::stoi转换为 intint
      std::stol转换为 longlong
      std::stoll转换为 long longlong long
      std::stoul转换为 unsigned longunsigned long
      std::stoull转换为 unsigned long longunsigned long long

      6. 结论

      • std::stoul 是 std::strtoul 的封装,支持进制转换。
      • 提供 pos 参数记录转换位置。
      • 可能抛出 std::invalid_argument 和 std::out_of_range 异常。
      • 适用于转换无符号整数,如 unsigned long,如果数值更大,可以使用 std::stoull

      到此这篇关于深入解析 C++中std::stoul 函数的文章就介绍到这了,更多相关C++ std::stoul 内容请搜索编程客栈(www.devze.com)以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程客栈(www.devze.com)! 

      0

      上一篇:

      下一篇:

      精彩评论

      暂无评论...
      验证码 换一张
      取 消

      最新开发

      开发排行榜