C++ String stol function
The std::stol function in C++ is used to convert a string representation of an integer into a long int. This function is part of the C++11 standard and is included in the <string> header.
Syntax
long stol(const std::string& str, std::size_t* idx = 0, int base = 10);
Parameters
| Parameter | Description |
|---|---|
str | The string containing the representation of an integral number. |
idx (optional) | A pointer to a std::size_t object. If provided, it will be set to the position of the first character in str after the number. 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: prefixes like 0x or 0X indicate base 16, 0 indicates base 8, and no prefix indicates base 10. Defaults to 10. |
Return Value
Upon successful conversion, std::stol returns the converted integral number as a value of type long int. If the conversion fails, it throws an exception.
Exceptions
The std::stol function can throw the following exceptions:
- std::invalid_argument: Thrown if no conversion could be performed because the input string does not contain a valid integral number.
- std::out_of_range: Thrown if the converted value falls outside the range of the
long inttype.
Examples for string stol function
Example 1: Converting a Decimal String to Long Integer
In this example, we convert a string containing a decimal number to a long int using std::stol.
Program:
#include <iostream>
#include <string>
int main() {
std::string str = "1234567890";
long num = std::stol(str);
std::cout << "The long integer value is: " << num << std::endl;
return 0;
}
Output:
The long integer value is: 1234567890
Explanation:
- The program includes the necessary headers:
<iostream>for input and output operations, and<string>for string handling. - A string
stris initialized with the value"1234567890". - The
std::stolfunction converts the string to along intand stores it in the variablenum. - The program outputs the long integer value using
std::cout.
Example 2: Converting a Hexadecimal String to Long Integer
In this example, we convert a string representing a hexadecimal number to a long int by specifying the base as 16.
Program
#include <iostream>
#include <string>
int main() {
std::string str = "7B";
long num = std::stol(str, nullptr, 16); // Base 16 indicates hexadecimal conversion
std::cout << "The long integer value is: " << num << std::endl;
return 0;
}
Output
The long integer value is: 123
Explanation
- The program includes the necessary headers:
<iostream>for input and output operations, and<string>for string handling. - The string
stris initialized with the hexadecimal value"7B". - The
std::stolfunction converts the string to a `long` integer, interpreting it as a base-16 (hexadecimal) value. - The result is stored in the variable
num, and the program prints it to the console.
Examples for Exceptions in std::stol
The std::stol function can throw exceptions if the string cannot be converted to a long due to invalid input or out-of-range values. Below are examples demonstrating these exceptions:
Example 1: Handling std::invalid_argument
This exception is thrown if the string does not contain a valid numeric representation.
Program
#include <iostream>
#include <string>
#include <exception>
int main() {
try {
std::string invalidStr = "HelloWorld";
long num = std::stol(invalidStr);
} catch (const std::invalid_argument& e) {
std::cout << "Invalid argument exception: " << e.what() << std::endl;
}
return 0;
}
Output
Invalid argument exception: stol
Explanation
- The string
invalidStrdoes not contain any valid numeric characters. - When
std::stolis called, it detects the invalid input and throws astd::invalid_argumentexception. - The exception is caught in the
catchblock, and an error message is displayed.
Example 2: Handling std::out_of_range
This exception is thrown if the value represented by the string exceeds the range of the long type.
Program
#include <iostream>
#include <string>
#include <exception>
int main() {
try {
std::string largeStr = "999999999999999999999";
long num = std::stol(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: stol
Explanation
- The string
largeStrcontains a numeric value that exceeds the range of along. - When
std::stolattempts to convert the string, it detects the out-of-range value and throws astd::out_of_rangeexception. - The exception is caught in the
catchblock, and an appropriate error message is displayed.
