C++ String stoll
The std::stoll
function in C++ converts a string representation of a signed integral number into a long long
. It is part of the C++11 standard and is defined in the <string>
header. This function is generally used for parsing strings containing large numerical values, such as those read from files or user input.
Syntax of string stoll function
long long stoll(const std::string& str, std::size_t* idx = 0, int base = 10);
Parameters of string stoll function
Parameter | Description |
---|---|
str | The string containing the representation of a signed 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 stoll function
On successful conversion, std::stoll
returns the converted value as a long long
. If the conversion fails, it throws an exception.
Exceptions for string stoll function
The std::stoll
function can throw the following exceptions:
- std::invalid_argument: Thrown if the input string does not contain a valid signed integral number.
- std::out_of_range: Thrown if the converted value exceeds the range of the
long long
type.
Examples for string stoll function
Example 1: Converting a Decimal String to Long Long
In this example, we convert a string containing a decimal number to a long long
using std::stoll
.
Program
#include
#include
int main() {
std::string str = "9223372036854775807"; // Maximum value for long long
long long num = std::stoll(str);
std::cout << "The long long value is: " << num << std::endl;
return 0;
}
Output
The long long value is: 9223372036854775807
Explanation
- The program includes the necessary headers: for input and output operations, and for string handling.
- A string
str
is initialized with the value"9223372036854775807"
, which is the maximum value for along long
. - The
std::stoll
function converts the string to along long
and stores it in the variablenum
. - The program outputs the long long value using
std::cout
.
Example 2: Converting a Hexadecimal String to Long Long
In this example, we convert a string representing a hexadecimal number to a long long
by specifying the base as 16.
Program
#include
#include
int main() {
std::string str = "0x7FFFFFFFFFFFFFFF"; // Maximum value for long long in hexadecimal
long long num = std::stoll(str, nullptr, 16);
std::cout << "The long long value is: " << num << std::endl;
return 0;
}
Output
The long long value is: 9223372036854775807
Explanation
- The string
str
contains the hexadecimal representation of the maximumlong long
value. - The
std::stoll
function is called with the base parameter set to 16, converting the hexadecimal string to a decimallong long
. - The resulting
long long
value is stored innum
and printed usingstd::cout
.
Examples for Exception Handling in stoll
Example 1: Handling std::invalid_argument
This exception is thrown when the string does not contain a valid numeric representation.
Program
#include
#include
#include
int main() {
try {
std::string invalidStr = "HelloWorld";
long long num = std::stoll(invalidStr);
} catch (const std::invalid_argument& e) {
std::cout << "Invalid argument exception: " << e.what() << std::endl;
}
return 0;
}
Output
Invalid argument exception: stoll
Explanation
- The string
invalidStr
contains non-numeric characters, making it an invalid input. - When
std::stoll
is called, it throws astd::invalid_argument
exception. - The exception is caught in the
catch
block, and an appropriate error message is displayed.
Example 2: Handling std::out_of_range
This exception is thrown when the value represented by the string exceeds the range of the long long
type.
Program
#include
#include
#include
int main() {
try {
std::string largeStr = "99999999999999999999";
long long num = std::stoll(largeStr);
} catch (const std::out_of_range& e) {
std::cout << "Out of range exception: " << e.what() << std::endl;
}
return 0;
}
Output
Out of range exception: stoll
Explanation
- The string
largeStr
contains a value that exceeds the range of along long
. - When
std::stoll
attempts to convert the string, it throws astd::out_of_range
exception. - The exception is caught in the
catch
block, and an appropriate error message is displayed.