C++ String stoull
The std::stoull
function in C++ converts a string representation of an unsigned integral number into an unsigned long long
. It is part of the C++11 standard and is defined in the <string>
header. This function is useful for parsing strings that represent large numerical values, especially when dealing with user input or data read from files.
Syntax of string stoull function
unsigned long long stoull(const std::string& str, std::size_t* idx = 0, int base = 10);
Parameters of string stoull function
Parameter | Description |
---|---|
str | The string containing the representation of an unsigned integral number. |
idx (optional) | A pointer to a std::size_t object. If provided, it is set to the position of the first character in str after the numerical value. Defaults to 0 . |
base (optional) | The numerical base (radix) for the conversion, which can range from 2 to 36. If set to 0 , the base is automatically deduced from the string format (e.g., prefixes like 0x indicate base 16). Defaults to 10 . |
Return Value of string stoull function
On successful conversion, std::stoull
returns the converted value as an unsigned long long
. If the conversion fails, it throws an exception.
Exceptions for string stoull function
The std::stoull
function can throw the following exceptions:
- std::invalid_argument: Thrown if the input string does not contain a valid unsigned integral number.
- std::out_of_range: Thrown if the converted value exceeds the range of the
unsigned long long
type.
Examples for string stoull function
Example 1: Converting a Decimal String to Unsigned Long Long
In this example, we convert a string containing a decimal number to an unsigned long long
using std::stoull
.
Program
#include <iostream>
#include <string>
int main() {
std::string str = "18446744073709551615"; // Maximum value for unsigned long long
unsigned long long num = std::stoull(str);
std::cout << "The unsigned long long value is: " << num << std::endl;
return 0;
}
Output
The unsigned long long value is: 18446744073709551615
Explanation
- The program includes the necessary headers:
<iostream>
for input and output operations, and<string>
for string handling. - A string
str
is initialized with the value"18446744073709551615"
, which is the maximum value for anunsigned long long
. - The
std::stoull
function converts the string to anunsigned long long
and stores it in the variablenum
. - The program outputs the unsigned long long value using
std::cout
.
Example 2: Converting a Hexadecimal String to Unsigned Long Long
In this example, we convert a string representing a hexadecimal number to an unsigned long long
by specifying the base as 16.
Program
#include <iostream>
#include <string>
int main() {
std::string str = "0xFFFFFFFFFFFFFFFF"; // Maximum value for unsigned long long in hexadecimal
unsigned long long num = std::stoull(str, nullptr, 16);
std::cout << "The unsigned long long value is: " << num << std::endl;
return 0;
}
Output
The unsigned long long value is: 18446744073709551615
Explanation
- The string
str
contains the hexadecimal representation of the maximumunsigned long long
value. - The
std::stoull
function is called with the base parameter set to 16, converting the hexadecimal string to a decimalunsigned long long
. - The resulting
unsigned long long
value is stored innum
and printed usingstd::cout
.
Examples for Exception Handling in stoull
Example 1: Handling std::invalid_argument
This exception is thrown when the string does not contain a valid numeric representation.
Program
#include <iostream>
#include <string>
#include <exception>
int main() {
try {
std::string invalidStr = "HelloWorld";
unsigned long long num = std::stoull(invalidStr);
} catch (const std::invalid_argument& e) {
std::cout << "Invalid argument exception: " << e.what() << std::endl;
}
return 0;
}
Output
Invalid argument exception: stoull
Explanation
- The string
invalidStr
contains non-numeric characters, making it an invalid input. - When
std::stoull
is called, it attempts to convert the string to anunsigned long long
, but since the string does not represent a valid number, the function throws astd::invalid_argument
exception. - The exception is caught in the
catch
block, and an appropriate error message is displayed usingstd::cout
.