std::stoul
(String to Unsigned Long) is a string conversion function in the C++ standard library, which is used to convertstd::string
orstd::wstring
Convert tounsigned long
Integer of type and supports conversion of different digits (such as binary, decimal, hexadecimal, etc.).
1. Basic introduction to std::stoul
std::stoul
Defined in<string>
in the header file and located instd
Under namespace. Its main function is to convert strings tounsigned long
Integer of type and supports conversion of different digits
1.1 Function prototype of std::stoul
unsigned long stoul(const std::string& str, size_t* pos = nullptr, int base = 10); unsigned long stoul(const std::wstring& str, size_t* pos = nullptr, int base = 10);
1.2 Parameter analysis
-
str
: The string to be converted must be in a valid unsigned integer format. -
pos
(Optional): If providedpos
,stoul
Will be here*pos
Storages the position where the conversion ends (i.e. the next position of the last converted character). -
base
(Optional): represents the binary system used by the string, the default value is10
(decimal), supported2
arrive36
Binary.
1.3 Return value
-
success: Return to the converted
unsigned long
Type integer. -
fail: If the string is not a valid number or exceeds
unsigned long
The range of type will throw an exception.
2. The implementation principle of std::stoul
In the GCC standard library<string>
In the header file,std::stoul
The underlying implementation is based onstd::strtoul
,as follows:
inline unsigned long stoul(const string& __str, size_t* __idx = 0, int __base = 10) { return __gnu_cxx::__stoa(&std::strtoul, "stoul", __str.c_str(), __idx, __base); }
2.1 parse implementation code
- Call
std::strtoulstd::strtoul
(String to Unsigned Long) is a standard library function in C language, which can convert C-style strings intounsigned long
Integer of type, and also supports binary conversion. - use
__gnu_cxx::__stoa
Contain packaging__stoa
is a tool function in the GNU C++ extension library that is used to convertunsigned long
Convert tounsigned long
, and perform error checks. -
__str.c_str()std::string
of.c_str()
Method returns oneconst char*
,Willstd::string
Convert to C-style strings so thatstd::strtoul
Analysis.
3. The actual usage of std::stoul
3.1 Basic examples
#include <iostream> #include <string> int main() { std::string str = "123456789"; unsigned long num = std::stoul(str); // Default decimal std::cout << "Conversion result: " << num << std::endl; return 0; }
Output:
Conversion result: 123456789
3.2 Use pos parameter to record the end of the conversion
#include <iostream> #include <string> int main() { std::string str = "12345xyz"; std::size_t pos; unsigned long num = std::stoul(str, &pos); std::cout << "Conversion result: " << num << std::endl; std::cout << "Conversion end position: " << pos << std::endl; return 0; }
Output:
Conversion result: 12345
Conversion end position: 5
Analysis:
-
stoul
Analysis"12345"
Later encountered"xyz"
, stop the conversion, andpos
Set as5
。
3.3 Handling conversions of different digits
#include <iostream> #include <string> int main() { std::string str = "1A"; // Hexadecimal unsigned long num = std::stoul(str, nullptr, 16); // parse in hexadecimal std::cout << "Conversion result: " << num << std::endl; return 0; }
Output:
Conversion result: 26
Analysis:
-
1A
In hexadecimal, equal to26
,sostoul
return26
。
Other Category Examples
std::stoul("1010", nullptr, 2); // parse in binary and return 10std::stoul("75", nullptr, 8); // parse in octal, return 61std::stoul("1F", nullptr, 16); // parse in hexadecimal, return 31
4. Handle exceptions
4.1 The input is not a valid number
ifstr
Not a valid integer format.std::stoul
Will be thrownstd::invalid_argument
Exception:
#include <iostream> #include <string> #include <stdexcept> int main() { try { std::string str = "abc"; unsigned long num = std::stoul(str); std::cout << "Conversion result: " << num << std::endl; } catch (const std::invalid_argument& e) { std::cerr << "Error: Invalid numeric string -> " << () << std::endl; } return 0; }
Output:
Error: Invalid numeric string -> stoul
4.2 The value exceeds the unsigned long range
If the value represented by the string exceedsunsigned long
The scope ofstd::stoul
Will be thrownstd::out_of_range
Exception:
#include <iostream> #include <string> #include <climits> int main() { try { std::string str = "99999999999999999999"; unsigned long num = std::stoul(str); std::cout << "Conversion result: " << num << std::endl; } catch (const std::out_of_range& e) { std::cerr << "Error: Values out of range -> " << () << std::endl; } return 0; }
Output:
Error: Values are out of range -> stoul
5. std::stoul related functions
function | effect | Return type |
---|---|---|
std::stoi |
Convert toint
|
int |
std::stol |
Convert tolong
|
long |
std::stoll |
Convert tolong long
|
long long |
std::stoul |
Convert tounsigned long
|
unsigned long |
std::stoull |
Convert tounsigned long long
|
unsigned long long |
6. Conclusion
-
std::stoul
yesstd::strtoul
encapsulation, supports binary conversion. - supply
pos
Parameter record conversion position. - Possible to throw
std::invalid_argument
andstd::out_of_range
Exception. - Suitable for converting unsigned integers, such as
unsigned long
, if the value is larger, you can usestd::stoull
。
This is the article about in-depth analysis of the std::stoul function in C++. For more related C++ std::stoul content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!