function template
<string>

std::stoll

long long stoll (const string&  str, size_t* idx = 0, int base = 10);
long long stoll (const wstring& str, size_t* idx = 0, int base = 10);
Convert string to long long
Parses str interpreting its content as an integral number of the specified base, which is returned as a value of type long long.

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 strtoll (or wcstoll) 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 long.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// stoll example
#include <iostream>   // std::cout
#include <string>     // std::string, std::stoll

int main ()
{
  std::string str = "8246821 0xffff 020";

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

  while (!str.empty()) {
    long long ll = std::stoll (str,&sz,0);
    std::cout << str.substr(0,sz) << " interpreted as " << ll << '\n';
    str = str.substr(sz);
  }

  return 0;
}


Output:
8246821 interpreted as 8246821
 0xffff interpreted as 65535
 020 interpreted as 16

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 long, an out_of_range exception is thrown.

An invalid idx causes undefined behavior.

See also