unsigned long stoul (const string& str, size_t* idx = 0, int base = 10);
unsigned long stoul (const wstring& str, size_t* idx = 0, int base = 10);
Convert string to unsigned integer
Parses str interpreting its content as an integral number of the specified base, which is returned as an unsigned long 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.
The function uses strtoul (or wcstoul) to perform the conversion (see strtol for more details on the process).
- String object with the representation of an integral number.
- Pointer to an object of type size_t, whose value is set by the function to position of the next character in str after the numerical value.
This parameter can also be a null pointer, in which case it is not used.
- Numerical base (radix) that determines the valid characters and their interpretation.
If this is 0, the base used is determined by the format in the sequence (see strtol for details). Notice that by default this argument is 10, not 0.
On success, the function returns the converted integral number as an unsigned long value.
// stoul example
#include <iostream> // std::cin, std::cout
#include <string> // std::string, std::stoul, std::getline
int main ()
std::cout << "Enter an unsigned number: ";
unsigned long ul = std::stoul (str,nullptr,0);
std::cout << "You entered: " << ul << '\n';
Unspecified, but generally linear in the number of characters interpreted.
Modifies the value pointed by idx (if not zero).
If no conversion could be performed, an invalid_argument exception is thrown.
If the value read is out of the range of representable values by an unsigned int, an out_of_range exception is thrown.
An invalid idx causes undefined behavior.
- Convert string to integer (function template
- Convert string to long int (function template
- Convert string to unsigned long long (function template
- Convert string to unsigned long integer (function