stoi

Converts a character sequence to an integer.

int stoi(
    const string& _Str, 
    size_t *_Idx = 0,
    int _Base = 10
);
int stoi(
    const wstring& _Str, 
    size_t *_Idx = 0,
    int _Base = 10
);

Return Value

The integer value.

Parameters

Parameter

Description

_Str

The character sequence to be converted.

_Idx

Contains the index of the first unconverted character on return.

_Base

The number base to use.

Remarks

The function stoi converts the sequence of characters in _Str to a value of type int and returns the value. For example, when passed a character sequence "10", the value returned by stoi is the integer 10.

stoi behaves similarly to the function strtol for single-byte characters when it is called in the manner strtol(_Str.c_str(), _Eptr, _Base), where _Eptr is an object internal to the function; or wcstol for wide characters, when it is called in similar manner, wcstol(Str.c_str(), _Eptr, _Base). For more information, see strtol, wcstol, _strtol_l, _wcstol_l.

If _Str.c_str() == *_Eptr, stoi throws an object of type invalid_argument. If such a call would set errno, or if the returned valuecannot be represented as an object of type int, it throws an object of type out_of_range. Otherwise, if _Idx is not a null pointer, the function stores *_Eptr - __Str.c_str() in *_Idx.

Requirements

Header: <string>

Namespace: std

See Also

Reference

string (<string>)

wstring

<string>

Other Resources

<string> Members