std::stoi Function in C++

Last Updated : 17 Jul, 2026

The std::stoi() function converts a string containing numeric characters into an integer value. It is a member of the C++ Standard Library and is declared in the <string> header.

  • Provides a simple and efficient way to perform string-to-integer conversion.
  • Supports different number bases such as decimal, hexadecimal, and binary.

Example: Basic Conversion

C++
#include <iostream>
#include <string>
using namespace std;

int main() {

    string str = "2009, GeeksforGeeks";

    int num = stoi(str);

    cout << num;

    return 0;
}

Output
2009

Explanation: stoi() converts the numeric prefix (2009) and stops when it reaches the comma.

Syntax

int stoi(const string& str,
size_t* pos = nullptr,
int base = 10);

Parameters

  • str: String to be converted.
  • pos: Stores the index of the first unprocessed character (optional).
  • base: Number base used for conversion. Default is 10

Return Value: returns the integer represented by the given string.

Working of std::stoi()

The std::stoi() function reads the string from left to right and converts the valid numeric portion into an integer. The conversion continues until an invalid character is encountered or the entire numeric value has been processed.

  • Ignores any leading whitespace characters before starting the conversion.
  • Converts consecutive numeric characters and stops at the first invalid character.
  • Supports number bases from 2 to 36, with 10 as the default base.
  • Throws an exception if the conversion fails.

Example: Using Different Bases

C++
#include <iostream>
#include <string>
using namespace std;

int main() {

    string hex = "6C1";
    string bin = "-10010010101";

    cout << stoi(hex, nullptr, 16) << endl;
    cout << stoi(bin, nullptr, 2);

    return 0;
}

Output
1729
-1173

Explanation

  • The first string is interpreted as a hexadecimal number.
  • The second string is interpreted as a binary number.

Exception Handling

std::stoi() throws the following exceptions when conversion fails.

ExceptionDescription
std::invalid_argumentThrown when the string does not begin with a valid number.
std::out_of_rangeThrown when the converted value cannot fit into an int.
C++
#include <iostream>
#include <string>
using namespace std;

int main() {

    string str = "GeeksforGeeks";

    try {
        cout << stoi(str);
    }
    catch (const invalid_argument&) {
        cout << "Invalid numeric string";
    }

    return 0;
}

Output
Invalid numeric string

Explanation: Since the string does not start with a numeric value, stoi() throws std::invalid_argument.

Complexity Analysis

  • Time Complexity: O(n), where n is the length of the string.
  • Auxiliary Space: O(1)

Applications

The std::stoi() function is widely used whenever integer values need to be extracted from strings during program execution.

  • Converts numeric values read from user input into integers for further processing.
  • Parses configuration files, CSV files, and other text-based data containing numbers.
  • Extracts numeric values from mixed strings while ignoring trailing characters.
  • Used in file processing, competitive programming, and data parsing tasks.

Best Practices

Following these practices helps make string-to-integer conversion safer and more reliable.

  • Always wrap std::stoi() inside a try-catch block when processing external or user-provided input.
  • Validate or sanitize the input string before attempting the conversion whenever possible.
  • Use the pos parameter if the string contains both numeric and non-numeric data.
  • Prefer std::stol() or std::stoll() when working with values larger than the int range.
Comment