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

</>
Copy
long stol(const std::string& str, std::size_t* idx = 0, int base = 10);

Parameters

ParameterDescription
strThe 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.
std::stol Parameters

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 int type.

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:

</>
Copy
#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:

  1. The program includes the necessary headers: <iostream> for input and output operations, and <string> for string handling.
  2. A string str is initialized with the value "1234567890".
  3. The std::stol function converts the string to a long int and stores it in the variable num.
  4. 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

</>
Copy
#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

  1. The program includes the necessary headers: <iostream> for input and output operations, and <string> for string handling.
  2. The string str is initialized with the hexadecimal value "7B".
  3. The std::stol function converts the string to a `long` integer, interpreting it as a base-16 (hexadecimal) value.
  4. 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

</>
Copy
#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

  1. The string invalidStr does not contain any valid numeric characters.
  2. When std::stol is called, it detects the invalid input and throws a std::invalid_argument exception.
  3. The exception is caught in the catch block, 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

</>
Copy
#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

  1. The string largeStr contains a numeric value that exceeds the range of a long.
  2. When std::stol attempts to convert the string, it detects the out-of-range value and throws a std::out_of_range exception.
  3. The exception is caught in the catch block, and an appropriate error message is displayed.