C++ std::stoi() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
std::stoi

Definition

In C++, std::stoi() reads an int from the beginning of a string.

It skips leading whitespace, then parses an optional sign and digits. Include <string>.

This is a free function from <string> that works with std::string (and sometimes wstring). Member functions like find live on the string object with a dot; helpers like this are called as std::stoi(...).

Syntax

Pass the string; optionally capture where parsing stopped and the base.

C++
int n = std::stoi("42");
int x = std::stoi("  -17");
int h = std::stoi("ff", nullptr, 16);
CallWhat it does
std::stoi(str)Parse str as a base-10 int.
std::stoi(str, &idx, base)Also store the end index and use base (2..36).

Related: stol, stoll for longer integers; stoul/stoull for unsigned.

Library declarations
C++
int stoi(const string& str, size_t* idx = 0, int base = 10);
int stoi(const wstring& str, size_t* idx = 0, int base = 10);

Parameters

ParameterDescription
strInput text.
idxOptional. Receives index of the first unconverted character.
baseNumeric base 2–36, or 0 for auto detect. Default 10.

Return Value

Return ValueDescription
intThe parsed value.

C++ Examples

Example 1 — Basic stoi()

Convert a simple number string.

C++
#include <iostream>
#include <string>

int main() {
    int n = std::stoi("42");
    std::cout << n << '\\n';
    return 0;
}

How It Works

  1. The text starts with digits.
  2. stoi parses them as base 10.
  3. Result is integer 42.

Example 2 — stoi End Index

See where parsing stopped.

C++
#include <iostream>
#include <string>

int main() {
    std::size_t idx = 0;
    int n = std::stoi("123abc", &idx);
    std::cout << n << ' ' << idx << '\\n';
    return 0;
}

How It Works

  1. Digits 123 are converted.
  2. Parsing stops at 'a'.
  3. idx is 3.

Example 3 — stoi Throws

Catch invalid_argument.

C++
#include <iostream>
#include <string>

int main() {
    try {
        std::stoi("abc");
    } catch (const std::invalid_argument&) {
        std::cout << "not a number\\n";
    }
    return 0;
}

How It Works

  1. There are no leading digits to parse.
  2. stoi throws invalid_argument.
  3. The handler prints a message.

Frequently Asked Questions

It converts a string to int.
invalid_argument and out_of_range.
Yes—pass base 16.
It parses a prefix. Use the idx parameter to see what was left.
stoi is C++, throws on error, and works with std::string. atoi is C and quieter on errors.

Next: stol()

Keep going through the string functions. Convert a string to long.

stol() tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

14 people found this page helpful