Javatpoint Logo
Javatpoint Logo

std::stoll() Function in C++

In this article, you will learn about the std::stoll function in C++ with its syntax and examples.

What is std::stoll()?

The C++ Standard Template Library (STL) contains the std::stoll() function, which is used particularly to convert strings to long long numbers. It is quite helpful when working with enormous numerical numbers that might not fit into the range of a conventional long integer. Strings can be converted to long long integers using the std::stoll() function. While it yields a long long integer instead of std::stol(), it functions exactly the same.

Syntax:

It has the following syntax:

long long std::stoll(const std::string& str, size_t* pos = 0, int base = 10);

str: The text that has to be changed into an extended long integer.

Pos: The index of the string's first unconverted character, stored as a pointer to a size_t object. If it is not required, this optional parameter can be set to nullptr.

Base: The conversion's numerical base. Ten is the default.

Return Value: The function extracts the numeric value from the input string and returns a long long integer as a result.

Exception: Like std::stol(), the function raises exceptions if the conversion cannot be completed or if any characters in the input string are invalid. The possible exceptions are std::invalid_argument in the event that no conversion could be completed and std::out_of_range in the event that the converted value is too large to be representable.

Use Cases:

  1. Managing Big Numerical Amounts:
    • When working with big numerical values that might go outside of the representable range of a normal long integer.
  2. Data Processing:
    • Data processing is helpful when receiving or storing numerical data in string format and needing to be translated before computation can take place.
  3. Data Validation:
    • Data validation is the process of confirming that input from users or data read from other sources are accurate long long integers.

Example Program 1:

Let's take an example to illustrate the use of std::stoll function in C++.

Output:

std::stoll() Function in C++

Example Program 2:

Let's take another example to illustrate the use of std::stoll function in C++.

Output:

std::stoll() Function in C++

Explanation:

  1. Opening of File:
    • An attempt is made by the program to read a file called "numeric_data.txt".
    • An error message reflecting the failure is printed to the standard error if the file cannot be opened.
    • After that, the application indicates that there was a problem opening the file by returning an error code of 1.
  2. Reading Text from the Document:
    • The program to hold strings initializes an empty vector called numericStrings.
    • After that, it starts reading every line from the opened file (inputFile) in a loop.
    • It saves the content in the currentLine string for every line.
    • Next, the numericStrings vector is expanded to include the currentLine.
    • Until the file has no more lines, this operation is repeated.
  3. Closing of File:
    • String the close() command, the file is closed once all of its lines have been read.
  4. Handling Numerical Strings and Finding the Sum:
    • The program initializes a variable called sum to track the total of the converted values.
    • Every string in numericStrings is iterated over.
    • It tries to use std::stoll to turn each string into a long long.
    • The converted value is added to the total if the conversion is successful.
    • It captures the exception, prints an error message, and moves on to the next string in the event of an exception (invalid argument or out of range).
  5. Producing the Total:
    • After that, the total of the converted values is printed to the standard output by the program.






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA