A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://cplusplus.com/reference/string/stol/ below:

function template

<string>

std::stol
long stol (const string&  str, size_t* idx = 0, int base = 10);long stol (const wstring& str, size_t* idx = 0, int base = 10);

Convert string to long int

Parses str interpreting its content as an integral number of the specified base, which is returned as a value of type long int.

If idx is not a null pointer, the function also sets the value of idx to the position of the first character in str after the number.

The function uses strtol (or wcstol) to perform the conversion (see strtol for more details on the process).



Parameters
str
String object with the representation of an integral number.
idx
Pointer to an object of type size_t, whose value is set by the function to position of the next character in str after the numerical value.
This parameter can also be a null pointer, in which case it is not used.
base
Numerical base (radix) that determines the valid characters and their interpretation.
If this is 0, the base used is determined by the format in the sequence (see strtol for details). Notice that by default this argument is 10, not 0.

Return Value On success, the function returns the converted integral number as a value of type long int.

Example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// stol example
#include <iostream>   // std::cout
#include <string>     // std::string, std::stol

int main ()
{
  std::string str_dec = "1987520";
  std::string str_hex = "2f04e009";
  std::string str_bin = "-11101001100100111010";
  std::string str_auto = "0x7fffff";

  std::string::size_type sz;   // alias of size_t

  long li_dec = std::stol (str_dec,&sz);
  long li_hex = std::stol (str_hex,nullptr,16);
  long li_bin = std::stol (str_bin,nullptr,2);
  long li_auto = std::stol (str_auto,nullptr,0);

  std::cout << str_dec << ": " << li_dec << '\n';
  std::cout << str_hex << ": " << li_hex << '\n';
  std::cout << str_bin << ": " << li_bin << '\n';
  std::cout << str_auto << ": " << li_auto << '\n';

  return 0;
}

Output:
1987520: 1987520
2f04e009: 788848649
-11101001100100111010: -956730
0x7fffff: 8388607


Complexity Unspecified, but generally linear in the number of characters interpreted.

Data races Modifies the value pointed by idx (if not zero).

Exceptions If no conversion could be performed, an invalid_argument exception is thrown.

If the value read is out of the range of representable values by a long int, either an invalid_argument or an out_of_range exception is thrown.

An invalid idx causes undefined behavior.



See also
stoi
Convert string to integer (function template)
stoll
Convert string to long long (function template)
stoul
Convert string to unsigned integer (function template)
strtol
Convert string to long integer (function)

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4