C++ String stoi function

The std::stoi function in C++ converts a string to an integer. This function is part of the C++11 standard and is included in the <string> header. It is generally used for parsing strings containing integer representations.


Syntax

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

Parameters

ParameterDescription
strThe string containing the representation of an integer.
idx (optional)A pointer to a std::size_t object. If provided, it stores the index of the first character not converted. Defaults to 0.
base (optional)The numerical base (radix) for the conversion, ranging from 2 to 36. If set to 0, the base is deduced from the string’s prefix. Defaults to 10.

Return Value

The function returns the integer value obtained by converting the string. If the conversion fails, it throws an exception.


Exceptions

The std::stoi function can throw the following exceptions:

  • std::invalid_argument: Thrown if the input string does not contain a valid integer representation.
  • std::out_of_range: Thrown if the converted value falls outside the range of the int type.

Examples for string stoi function

Example 1: Converting a Decimal String to Integer

In this example, we convert a string containing a decimal number to an integer using std::stoi.

Program

</>
Copy
#include <iostream>
#include <string>

int main() {
    std::string str = "12345";
    int num = std::stoi(str);
    std::cout << "The integer value is: " << num << std::endl;
    return 0;
}

Output

The integer value is: 12345

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 "12345".
  3. The std::stoi function converts the string to an integer and stores it in the variable num.
  4. The program outputs the integer value using std::cout.

Example 2: Handling Base Conversion

In this example, we convert a string representing a hexadecimal number to an integer by specifying the base as 16.

Program

</>
Copy
#include <iostream>
#include <string>

int main() {
    std::string str = "1A3F";
    int num = std::stoi(str, nullptr, 16);
    std::cout << "The integer value is: " << num << std::endl;
    return 0;
}

Output

The integer value is: 6719

Explanation

  1. The string str contains a hexadecimal number "1A3F".
  2. The std::stoi function is called with the base parameter set to 16, converting the string into its decimal equivalent.
  3. The result is stored in num and printed using std::cout.

Example for Invalid Argument and Out of Range Exceptions

The following program demonstrates how exceptions like std::invalid_argument and std::out_of_range can occur when using std::stoi function.

main.cpp

</>
Copy
#include <iostream>
#include <string>
#include <exception>

int main() {
    try {
        std::string invalidStr = "Hello";
        int num = std::stoi(invalidStr);
    } catch (const std::invalid_argument& e) {
        std::cout << "Invalid argument exception: " << e.what() << std::endl;
    }

    try {
        std::string largeStr = "999999999999999999999";
        int num = std::stoi(largeStr);
    } catch (const std::out_of_range& e) {
        std::cout << "Out of range exception: " << e.what() << std::endl;
    }

    return 0;
}

Output

Invalid argument exception: stoi
Out of range exception: stoi

In the first try-catch block, we tried to convert an invalid string to integer.

In the second try-catch block, we tried to convert a number that is out of range for the limits of an integer.