# std::stoi, std::stol, std::stoll

< cpp‎ | string‎ | basic string

C++
 Language Standard library headers Concepts Utilities library Strings library Containers library Algorithms library Iterators library Numerics library Input/output library Localizations library Regular expressions library (C++11) Atomic operations library (C++11) Thread support library (C++11) Technical Specifications

Strings library
 Null-terminated strings Byte strings Multibyte strings Wide strings Classes basic_string char_traits

std::basic_string
 Member functions basic_string::basic_string basic_string::operator= basic_string::assign basic_string::get_allocator Element access basic_string::at basic_string::operator[] basic_string::front (C++11) basic_string::back (C++11) basic_string::data basic_string::c_str Iterators Capacity basic_string::empty basic_string::sizebasic_string::length basic_string::max_size basic_string::reserve basic_string::capacity basic_string::shrink_to_fit (C++11)
 Non-member functions operator+ operator==operator!=operatoroperator<=operator>= swap(std::basic_string) operator<> getline stoistolstoll (C++11)(C++11)(C++11) stoulstoull (C++11)(C++11) stofstodstold (C++11)(C++11)(C++11) to_string (C++11) to_wstring (C++11) Helper classes hashhashhashhash (C++11)(C++11)(C++11)(C++11)

 Defined in header `` int       stoi( const std::string& str, std::size_t* pos = 0, int base = 10 ); int       stoi( const std::wstring& str, std::size_t* pos = 0, int base = 10 ); (1) (since C++11) long      stol( const std::string& str, std::size_t* pos = 0, int base = 10 ); long      stol( const std::wstring& str, std::size_t* pos = 0, int base = 10 ); (2) (since C++11) long long stoll( const std::string& str, std::size_t* pos = 0, int base = 10 ); long long stoll( const std::wstring& str, std::size_t* pos = 0, 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 `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 digits for base-2 integer is `01`, for base-3 integer is `012`, 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 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

The string converted to the specified signed integer type.

### 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 <iostream>
#include <string>

int main()
{
std::string str1 = "45";
std::string str2 = "3.14159";
std::string str3 = "31337 with words";
std::string str4 = "words and 2";

int myint1 = std::stoi(str1);
int myint2 = std::stoi(str2);
int myint3 = std::stoi(str3);
// error: 'std::invalid_argument'
// int myint4 = std::stoi(str4);

std::cout << "std::stoi(\"" << str1 << "\") is " << myint1 << '\n';
std::cout << "std::stoi(\"" << str2 << "\") is " << myint2 << '\n';
std::cout << "std::stoi(\"" << str3 << "\") is " << myint3 << '\n';
//std::cout << "std::stoi(\"" << str4 << "\") is " << myint4 << '\n';
}```

Output:

```std::stoi("45") is 45
std::stoi("3.14159") is 3
std::stoi("31337 with words") is 31337```

 atoiatolatoll converts a byte string to an integer value (function) stoulstoull (C++11)(C++11) converts a string to an unsigned integer (function) stofstodstold (C++11)(C++11)(C++11) converts a string to a floating point value (function) to_string (C++11) converts an integral or floating point value to `string` (function)
{"ft_lang_label":"__label__en","ft_lang_prob":0.6441963,"math_prob":0.9741946,"size":3692,"snap":"2023-14-2023-23","text_gpt3_token_len":1051,"char_repetition_ratio":0.15509762,"word_repetition_ratio":0.106451616,"special_character_ratio":0.3280065,"punctuation_ratio":0.18890357,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.992865,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-04-01T22:40:01Z\",\"WARC-Record-ID\":\"<urn:uuid:5dc9586d-78e2-46f1-b9a7-895fc8ab600f>\",\"Content-Length\":\"47525\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:b5627e63-58ab-498a-858b-69f0387cd12a>\",\"WARC-Concurrent-To\":\"<urn:uuid:48d697da-b3d5-4f05-830c-d20310a79ccd>\",\"WARC-IP-Address\":\"47.104.236.95\",\"WARC-Target-URI\":\"https://doc.bccnsoft.com/docs/cppreference2015/en/cpp/string/basic_string/stol.html\",\"WARC-Payload-Digest\":\"sha1:2BBD6SSDC5RWC5N3B42JJP5MKVJ6E66I\",\"WARC-Block-Digest\":\"sha1:BGOFV4C3SDA4SCLXAGLK377AQNCK6KNR\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-14/CC-MAIN-2023-14_segments_1679296950363.89_warc_CC-MAIN-20230401221921-20230402011921-00203.warc.gz\"}"}