Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/string/basic string/stol"

From cppreference.com
< cpp‎ | string‎ | basic string
m (Exceptions: tt)
m (Example: extended to demo exceptions and invoke with non-default `pos` and `radix`)
Line 44: Line 44:
 
{{example|
 
{{example|
 
  | code=  
 
  | code=  
#include <iostream>
 
 
#include <string>
 
#include <string>
+
#include <iomanip>
 +
#include <iostream>
 +
#include <stdexcept>
 +
 
 
int main()
 
int main()
 
{
 
{
     std::string str1 = "45";
+
     const auto data = {
    std::string str2 = "3.14159";
+
        "45",
    std::string str3 = "31337 with words";
+
        "+45",
    std::string str4 = "words and 2";
+
        " -45",
      
+
        "3.14159",
     int myint1 = std::stoi(str1);
+
        "31337 with words",
     int myint2 = std::stoi(str2);
+
        "words and 2",
    int myint3 = std::stoi(str3);
+
        "12345678901",
    // error: 'std::invalid_argument'
+
     };
    // int myint4 = std::stoi(str4);
+
 
   
+
     for (const std::string s : data)
    std::cout << "std::stoi(\"" << str1 << "\") is " << myint1 << '\n';
+
     {
    std::cout << "std::stoi(\"" << str2 << "\") is " << myint2 << '\n';
+
        std::size_t pos{};
    std::cout << "std::stoi(\"" << str3 << "\") is " << myint3 << '\n';
+
        try
     //std::cout << "std::stoi(\"" << str4 << "\") is " << myint4 << '\n';
+
        {
 +
            std::cout << "std::stoi('" << s << "'): ";
 +
            const int i {std::stoi(s, &pos)};
 +
            std::cout << i << "; pos: " << pos << '\n';
 +
        }
 +
        catch(std::invalid_argument const& ex)
 +
        {
 +
            std::cout << "... std::invalid_argument::what(): " << ex.what() << '\n';
 +
        }
 +
        catch(std::out_of_range const& ex)
 +
        {
 +
            std::cout << "... std::out_of_range::what(): " << ex.what() << '\n';
 +
            const long long ll {std::stoll(s, &pos)};
 +
            std::cout << "std::stoll('" << s << "'): " << ll << "; pos = " << pos << '\n';
 +
        }
 +
    }
 +
 
 +
     std::cout << "\nCalling with different radixes:\n";
 +
    for (const int base: {2, 3, 4, 8, 10, 12, 16, 20, 36})
 +
    {
 +
        const int i {std::stoi("111", nullptr, base)};
 +
        std::cout << "std::stoi('111', " << base << "): " << i << '\n';
 +
    }
 
}
 
}
 +
| p=true
 
  | output=
 
  | output=
std::stoi("45") is 45
+
std::stoi('45'): 45; pos: 2
std::stoi("3.14159") is 3
+
std::stoi('+45'): 45; pos: 3
std::stoi("31337 with words") is 31337
+
std::stoi(' -45'): -45; pos: 4
 +
std::stoi('3.14159'): 3; pos: 1
 +
std::stoi('31337 with words'): 31337; pos: 5
 +
std::stoi('words and 2'): ... std::invalid_argument::what(): stoi
 +
std::stoi('12345678901'): ... std::out_of_range::what(): stoi
 +
std::stoll('12345678901'): 12345678901; pos = 11
 +
 
 +
Calling with different radixes:
 +
std::stoi('111', 2): 7
 +
std::stoi('111', 3): 13
 +
std::stoi('111', 4): 21
 +
std::stoi('111', 8): 73
 +
std::stoi('111', 10): 111
 +
std::stoi('111', 12): 157
 +
std::stoi('111', 16): 273
 +
std::stoi('111', 20): 421
 +
std::stoi('111', 36): 1333
 
}}
 
}}
  

Revision as of 15:47, 24 February 2022

 
 
 
std::basic_string
Member functions
Element access
Iterators
Capacity
Modifiers
Search
Operations
Constants
Non-member functions
I/O
Comparison
(until C++20)(until C++20)(until C++20)(until C++20)(until C++20)(C++20)
Numeric conversions
stoistolstoll
(C++11)(C++11)(C++11)
(C++11)(C++11)
(C++11)(C++11)(C++11)
(C++11)
(C++11)
Literals
Helper classes
Deduction guides (C++17)

 
Defined in header <string>
int       stoi( const std::string& str, std::size_t* pos = nullptr, int base = 10 );
int       stoi( const std::wstring& str, std::size_t* pos = nullptr, int base = 10 );
(1) (since C++11)
long      stol( const std::string& str, std::size_t* pos = nullptr, int base = 10 );
long      stol( const std::wstring& str, std::size_t* pos = nullptr, int base = 10 );
(2) (since C++11)
long long stoll( const std::string& str, std::size_t* pos = nullptr, int base = 10 );
long long stoll( const std::wstring& str, std::size_t* pos = nullptr, int base = 10 );
(3) (since C++11)

Interprets a signed integer value in the string str.

1) calls std::strtol(str.c_str(), &ptr, base) or std::wcstol(str.c_str(), &ptr, base)
2) calls std::strtol(str.c_str(), &ptr, base) or std::wcstol(str.c_str(), &ptr, base)
3) calls std::strtoll(str.c_str(), &ptr, base) or std::wcstoll(str.c_str(), &ptr, base)

Discards any whitespace characters (as identified by calling std::isspace) until the first non-whitespace character is found, then takes as many characters as possible to form a valid base-n (where n=base) integer number representation and converts them to an integer value. The valid integer value consists of the following parts:

  • (optional) plus or minus sign
  • (optional) prefix (0) indicating octal base (applies only when the base is 8 or 0)
  • (optional) prefix (0x or 0X) indicating hexadecimal base (applies only when the base is 16 or 0)
  • a sequence of digits

The set of valid values for base is {0,2,3,...,36}. The set of valid digits for base-2 integers is {0,1}, for base-3 integers is {0,1,2}, and so on. For bases larger than 10, valid digits include alphabetic characters, starting from Aa for base-11 integer, to Zz for base-36 integer. The case of the characters is ignored.

Additional numeric formats may be accepted by the currently installed C locale.

If the value of base is 0, the numeric base is auto-detected: if the prefix is 0, the base is octal, if the prefix is 0x or 0X, the base is hexadecimal, otherwise the base is decimal.

If the minus sign was part of the input sequence, the numeric value calculated from the sequence of digits is negated as if by unary minus in the result type.

If pos is not a null pointer, then a pointer ptr - internal to the conversion functions - will receive the address of the first unconverted character in str.c_str(), and the index of that character will be calculated and stored in *pos, giving the number of characters that were processed by the conversion.

Contents

Parameters

str - the string to convert
pos - address of an integer to store the number of characters processed
base - the number base

Return value

Integer value corresponding to the content of str.

Exceptions

  • std::invalid_argument if no conversion could be performed
  • std::out_of_range if the converted value would fall out of the range of the result type or if the underlying function (std::strtol or std::strtoll) sets errno to ERANGE.

Example

#include <string>
#include <iomanip>
#include <iostream>
#include <stdexcept>
 
int main()
{
    const auto data = {
        "45",
        "+45",
        " -45",
        "3.14159",
        "31337 with words",
        "words and 2",
        "12345678901",
    };
 
    for (const std::string s : data)
    {
        std::size_t pos{};
        try
        {
            std::cout << "std::stoi('" << s << "'): ";
            const int i {std::stoi(s, &pos)};
            std::cout << i << "; pos: " << pos << '\n';
        }
        catch(std::invalid_argument const& ex)
        {
            std::cout << "... std::invalid_argument::what(): " << ex.what() << '\n';
        }
        catch(std::out_of_range const& ex)
        {
            std::cout << "... std::out_of_range::what(): " << ex.what() << '\n';
            const long long ll {std::stoll(s, &pos)};
            std::cout << "std::stoll('" << s << "'): " << ll << "; pos = " << pos << '\n';
        }
    }
 
    std::cout << "\nCalling with different radixes:\n";
    for (const int base: {2, 3, 4, 8, 10, 12, 16, 20, 36})
    {
        const int i {std::stoi("111", nullptr, base)};
        std::cout << "std::stoi('111', " << base << "): " << i << '\n';
    }
}

Possible output:

std::stoi('45'): 45; pos: 2
std::stoi('+45'): 45; pos: 3
std::stoi(' -45'): -45; pos: 4
std::stoi('3.14159'): 3; pos: 1
std::stoi('31337 with words'): 31337; pos: 5
std::stoi('words and 2'): ... std::invalid_argument::what(): stoi
std::stoi('12345678901'): ... std::out_of_range::what(): stoi
std::stoll('12345678901'): 12345678901; pos = 11
 
Calling with different radixes:
std::stoi('111', 2): 7
std::stoi('111', 3): 13
std::stoi('111', 4): 21
std::stoi('111', 8): 73
std::stoi('111', 10): 111
std::stoi('111', 12): 157
std::stoi('111', 16): 273
std::stoi('111', 20): 421
std::stoi('111', 36): 1333

See also

(C++11)(C++11)
converts a string to an unsigned integer
(function) [edit]
(C++11)(C++11)(C++11)
converts a string to a floating point value
(function) [edit]
converts a byte string to an integer value
(function) [edit]
converts a byte string to an unsigned integer value
(function) [edit]
(C++11)(C++11)
converts a byte string to std::intmax_t or std::uintmax_t
(function) [edit]
converts a character sequence to an integer or floating-point value
(function) [edit]
converts a byte string to an integer value
(function) [edit]
(C++11)
converts an integral or floating-point value to string
(function) [edit]
converts an integral or floating-point value to wstring
(function) [edit]