std::stoi() takes first number in string?

According to the C++ standard(N4296),

§ 21.5

21.5 Numeric conversions [string.conversions]

  1. Effects: the first two functions call strtol(str.c_str(), ptr, base), and the last three functions
    call strtoul(str.c_str(), ptr, base), strtoll(str.c_str(), ptr, base), and strtoull(
    str.c_str(), ptr, base)
    , respectively. Each function returns the converted result, if any. The argument
    ptr designates a pointer to an object internal to the function that is used to determine what
    to store at *idx. If the function does not throw an exception and idx != 0, the function stores in
    *idx the index of the first unconverted element of str.
  2. Returns: The converted result.
  3. Throws: invalid_argument if strtol, strtoul, strtoll, or strtoull reports that no conversion
    could be performed. Throws out_of_range if strtol, strtoul, strtoll or strtoull sets errno to
    ERANGE, or if the converted value is outside the range of representable values for the return type.

What is strtol?

§ 21.8

21.8 Null-terminated sequence utilities [c.strings]

  1. Tables 74, 75, 76, 77, 78, and 79 describe headers <cctype>, <cwctype>, <cstring>, <cwchar>, <cstdlib>
    (character conversions), and <cuchar>, respectively.
  2. The contents of these headers shall be the same as the Standard C Library headers <ctype.h>, <wctype.h>,
    <string.h>, <wchar.h>, and <stdlib.h> and the C Unicode TR header <uchar.h>, respectively, with the
    following modifications:

Table 78 — Header <cstdlib> synopsis

Type Name(s)
Macros: MB_CUR_MAX
Functions:
atof mblen strtof strtoul
atoi mbtowc strtol strtoull
atol mbstowcs strtold wctomb
atoll strtod strtoll wcstombs

What the meaning of Standard C Library?

§ 1.2

1.2 Normative references [intro.refs]

  1. The following referenced documents are indispensable for the application of this document. For dated references,
    only the edition cited applies. For undated references, the latest edition of the referenced document
    (including any amendments) applies.

    (1.3) — ISO/IEC 9899:1999, Programming languages — C
    (1.4) — ISO/IEC 9899:1999/Cor.1:2001(E), Programming languages — C, Technical Corrigendum 1
    (1.5) — ISO/IEC 9899:1999/Cor.2:2004(E), Programming languages — C, Technical Corrigendum 2

  2. The library described in Clause 7 of ISO/IEC 9899:1999 and Clause 7 of ISO/IEC 9899:1999/Cor.1:2001
    and Clause 7 of ISO/IEC 9899:1999/Cor.2:2003 is hereinafter called the C standard library.1

So, we need to watch C99 standard.

According to the C99 standard(N1124)

§7.20.1.4

7.20.1.4 The strtol, strtoll, strtoul, and strtoull functions

  1. Description
    The strtol, strtoll, strtoul, and strtoull functions convert the initial portion of the string pointed to by nptr to long int, long long int, unsigned long int, and unsigned long long int representation, respectively. First, they decompose the input string into three parts: an initial, possibly empty, sequence of
    white-space characters (as specified by the isspace function), a subject sequence resembling an integer represented in some radix determined by the value of base, and a final string of one or more unrecognized characters, including the terminating null character of the input string. Then, they attempt to convert the subject sequence to an integer, and return the result.
  2. If the value of base is zero, the expected form of the subject sequence is that of an integer constant as described in 6.4.4.1, optionally preceded by a plus or minus sign, but not including an integer suffix. If the value of base is between 2 and 36 (inclusive), the expected form of the subject sequence is a sequence of letters and digits representing an integer with the radix specified by base, optionally preceded by a plus or minus sign,
    but not including an integer suffix. The letters from a (or A) through z (or Z) are ascribed the values 10 through 35; only letters and digits whose ascribed values are less than that of base are permitted. If the value of base is 16, the characters 0x or 0X may optionally precede the sequence of letters and digits, following the sign if present.
  3. The subject sequence is defined as the longest initial subsequence of the input string, starting with the first non-white-space character, that is of the expected form. The subject sequence contains no characters if the input string is empty or consists entirely of white space, or if the first non-white-space character is other than a sign or a permissible letter
    or digit.
  4. If the subject sequence has the expected form and the value of base is zero, the sequence of characters starting with the first digit is interpreted as an integer constant according to the rules of 6.4.4.1. If the subject sequence has the expected form and the value of base is between 2 and 36, it is used as the base for conversion, ascribing to each letter its value as given above. If the subject sequence begins with a minus sign, the value resulting from the conversion is negated (in the return type). A pointer to the final string is stored in the object pointed to by endptr, provided that endptr is not a null pointer.
  5. In other than the "C" locale, additional locale-specific subject sequence forms may be accepted.
  6. If the subject sequence is empty or does not have the expected form, no conversion is performed; the value of nptr is stored in the object pointed to by endptr, provided that endptr is not a null pointer.
  7. Returns
    The strtol, strtoll, strtoul, and strtoull functions return the converted value, if any. If no conversion could be performed, zero is returned. If the correct value is outside the range of representable values, LONG_MIN, LONG_MAX, LLONG_MIN, LLONG_MAX, ULONG_MAX, or ULLONG_MAX is returned (according to the return type and sign of the value, if any), and the value of the macro ERANGE is stored in errno.

Now we need to watch C++ standard again.

21.5 Numeric conversions [string.conversions]

  1. Throws: invalid_argument if strtol, strtoul, strtoll, or strtoull reports that no conversion
    could be performed. Throws out_of_range if strtol, strtoul, strtoll or strtoull sets errno to
    ERANGE, or if the converted value is outside the range of representable values for the return type.

Now you can understand when invalid_argument and out_of_range exception is thrown, I hope.

Leave a Comment