
- C++ Library - Home
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- The C++ STL Library
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- The C++ Advanced Library
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ STL Library Cheat Sheet
- C++ STL - Cheat Sheet
- C++ Programming Resources
- C++ Programming Tutorial
- C++ Useful Resources
- C++ Discussion
C++ String::stoll() function
The C++ std::string::stoll() function is used to convert a string to a long long integer. It is used to analyse numerical values from strings and handle large integers. This function takes two optional parameters: pos( to specify the starting position in the string) and base (to define the number base, such as decimal or hexadecimal).
Syntax
Following is the syntax for std::string::stoll() function.
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);
Parameters
- str − It indicates the string object with the representation of a integral number.
- idx − It indicates the 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.
- base − It indicates the numerical base that determines the valid character and their interpretation.
Return value
It returns a string as the value into a long long integer.
Example 1
Following is the basic example to convert a string into a long long integer using C++.
#include#include using namespace std; int main() { string str = "9223372036854775807"; long long int number = stoll(str); cout << number << endl; return 0; }
Output
If we run the above code it will generate the following output.
9223372036854775807
Example 2
In this example, we are passing a hexadecimal string to convert into a long int.
#include#include using namespace std; int main() { string hexStr = "7FFFFFFFFFFFFFFF"; long long number = stoll(hexStr, nullptr, 16); cout << number << endl; return 0; }
Output
If we run the above code it will generate the following output.
9223372036854775807
Example 3
In this program we are giving comment-line argument conversion to long long integer.
#include#include using namespace std; int main(int argc, char * argv[]) { if (argc != 2) { cerr << "Usage: " << argv[0] << " " << endl; return 1; } try { string argStr = argv[1]; long long number = stoll(argStr); cout << "The command-line argument \"" << argStr << "\" converted to long long is " << number << endl; } catch (const std::exception & e) { cerr << "Error: " << e.what() << endl; return 1; } return 0; }
Output
Following is the output of the above code.
Usage: main