std::stoul,std::stoull(3)
π std::stoul, std::stoull β the C++ Standard Library glow-up
okay bestie, welcome to <string>'s number-parsing corner π this is std::stoul and std::stoull, straight out of the C++ Standard Library, and we're gonna get you fluent, no cap β¨
πΈ NAME πΈ
std::stoul, std::stoull β literally just std::stoul,std::stoull π (yes the manpage is that self-referential, she's a little icon that way)
β¨ SYNOPSIS β¨
Defined in header <string> β that's the vibe check you need to pass first, iykyk π¦
(1) β since C++11 π₯
unsigned long stoul ( const std::string& str,
std::size_t* pos = nullptr, int base = 10 );
(2) β since C++11 π₯
unsigned long stoul ( const std::wstring& str,
std::size_t* pos = nullptr, int base = 10 );
(3) β since C++11 π₯
unsigned long long stoull( const std::string& str,
std::size_t* pos = nullptr, int base = 10 );
(4) β since C++11 π₯
unsigned long long stoull( const std::wstring& str,
std::size_t* pos = nullptr, int base = 10 );
π§ DESCRIPTION π§
So what she does: she interprets an unsigned integer value inside the string str. Main character energy π
Let ptr be an internal (to the conversion functions) pointer of type char* (1,3) or wchar_t* (2,4), accordingly. Lowkey important, keep that in your back pocket π«Ά
1)Callsstd::strtoul(str.c_str(), &ptr, base).2)Callsstd::wcstoul(str.c_str(), &ptr, base).3)Callsstd::strtoull(str.c_str(), &ptr, base).4)Callsstd::wcstoull(str.c_str(), &ptr, base).
She discards any whitespace characters (as identified by calling std::isspace) until the first non-whitespace character is found π then takes as many characters as possible to form a valid base-n (where n=base) unsigned integer number representation and converts them to an integer value. Obsessed with her efficiency, honestly.
The valid unsigned integer value consists of these parts, in this exact order π
- (optional) plus or minus sign
- (optional) prefix (
0) indicating octal base (applies only when the base is8or0) - (optional) prefix (
0xor0X) indicating hexadecimal base (applies only when the base is16or0) - a sequence of digits
The set of valid values for base is {0,2,3,...,36} β The set of valid digits for base-2 integers is {0,1}, for base-3 integers is {0,1,2}, and so on. For bases larger than 10, valid digits include alphabetic characters, starting from Aa for base-11 integer, to Zz for base-36 integer. The case of the characters is ignored π (she does not care about your capital letters, she's above it)
Additional numeric formats may be accepted by the currently installed C locale π (so likeβ¦ your mileage may vary depending on locale, ngl)
If the value of base is 0, the numeric base is auto-detected: if the prefix is 0, the base is octal, if the prefix is 0x or 0X, the base is hexadecimal, otherwise the base is decimal π she's intuitive like that.
β οΈ okay this part matters: if the minus sign was part of the input sequence, the numeric value calculated from the sequence of digits is negated as if by unary minus in the result type, which applies unsigned integer wraparound rules. π so like, yes you can pass a minus sign, and yes the result is still unsigned, and yes the math gets feral. Don't say I didn't warn you.
If pos is not a null pointer, then ptr will receive the address of the first unconverted character in str.c_str(), and the index of that character will be calculated and stored in *pos, giving the number of characters that were processed by the conversion π she's giving receipts.
π PARAMETERS π
strβ the string to convert πposβ address of an integer to store the number of characters processedbaseβ the number base
π RETURN VALUE π
The string converted to the specified unsigned integer type. That's it, that's the whole return, slay π
π EXCEPTIONS π
std::invalid_argumentif no conversion could be performed π (she tried, okay?)std::out_of_rangeif the converted value would fall out of the range of the result type or if the underlying function (std::strtoulorstd::strtoull) setserrnotoERANGE. π¨
π¦ EXAMPLE π¦
This section is incomplete π
Reason: no example
(likeβ¦ they really left us hanging, fr fr π)
β DEFECT REPORTS β
The following behavior-changing defect reports were applied retroactively to previously published C++ standards. History rewrite, but make it committee πβ¨
- DR
LWG 2009β Applied to:C++11β Behavior as published:std::out_of_rangewould not be thrown ifstd::strtoulorstd::strtoullsetserrnotoERANGEβ Correct behavior: will throw π₯
π SEE ALSO π
stoi,stol,stollβ converts a string to a signed integer (function) (C++11) (C++11) (C++11) πΈstof,stod,stoldβ converts a string to a floating point value (function) (C++11) (C++11) (C++11) π«Ά
π CATEGORY π
- Todo no example π
and that's the whole page, bestie π source: http://cppreference.com, version/date 2024.06.10, filed under std::stoul,std::stoull(3) β mwah, go parse something iconic ππ
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms