Namespaces
Variants

std:: stoul, std:: stoull

From cppreference.net
std::basic_string
定义于头文件 <string>
unsigned long stoul ( const std:: string & str,
std:: size_t * pos = nullptr, int base = 10 ) ;
(1) (C++11 起)
unsigned long stoul ( const std:: wstring & str,
std:: size_t * pos = nullptr, int base = 10 ) ;
(2) (C++11 起)
unsigned long long stoull ( const std:: string & str,
std:: size_t * pos = nullptr, int base = 10 ) ;
(3) (C++11 起)
unsigned long long stoull ( const std:: wstring & str,
std:: size_t * pos = nullptr, int base = 10 ) ;
(4) (C++11 起)

将字符串 str 中的内容解析为无符号整数值。

ptr 作为转换函数内部的指针,其类型为 char * (1,3) wchar_t * (2,4) (根据具体情况而定)。

1) 调用 std:: strtoul ( str. c_str ( ) , & ptr, base )
2) 调用 std:: wcstoul ( str. c_str ( ) , & ptr, base )
3) 调用 std:: strtoull ( str. c_str ( ) , & ptr, base )
4) 调用 std:: wcstoull ( str. c_str ( ) , & ptr, base )

丢弃所有空白字符(通过调用 std::isspace 识别),直到找到第一个非空白字符,然后尽可能多地提取字符以构成有效的 base-n (其中n= base )无符号整数表示形式,并将其转换为整数值。有效的无符号整数值由以下部分组成:

  • (可选) 正负号
  • (可选) 表示八进制基数的前缀 ( 0 )(仅当基数为 8 0 时适用)
  • (可选) 表示十六进制基数的前缀 ( 0x 0X )(仅当基数为 16 0 时适用)
  • 数字序列

基数的有效取值范围为 {0, 2, 3, ..., 36} 。对于基数为 2 的整数,有效数字集为 {0, 1} ;对于基数为 3 的整数,有效数字集为 {0, 1, 2} ,依此类推。对于大于 10 的基数,有效数字包含字母字符:从基数为 11 的整数使用 Aa 开始,到基数为 36 的整数使用 Zz 结束。字符的大小写将被忽略。

当前安装的 C locale 可能接受其他数字格式。

base 的值为 0 ,将自动检测数值进制:若前缀为 0 ,则为八进制;若前缀为 0x 0X ,则为十六进制;否则为十进制。

如果减号是输入序列的一部分,则从数字序列计算得出的数值将被取反,如同在结果类型中应用 一元减号 运算符,该运算遵循无符号整数回绕规则。

如果 pos 不是空指针,那么 ptr 将接收 str. c_str ( ) 中第一个未转换字符的地址,该字符的索引将被计算并存储在 * pos 中,表示转换处理的字符数量。

目录

参数

str - 要转换的字符串
pos - 用于存储已处理字符数的整数地址
base - 数值基数

返回值

转换为指定无符号整数类型的字符串。

异常

示例

缺陷报告

以下行为变更缺陷报告被追溯应用于先前发布的C++标准。

缺陷报告 适用标准 发布时行为 正确行为
LWG 2009 C++11
std::out_of_range 不会抛出当
std::strtoul std::strtoull 设置 errno ERANGE
将会抛出

参见

(C++11) (C++11) (C++11)
将字符串转换为有符号整数
(函数)
(C++11) (C++11) (C++11)
将字符串转换为浮点值
(函数)