Stdstoll 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::invalidargument in the event that no conversion could be completed and std::outof_range in the event that the converted value is too large to be representable.

Use Cases:

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

Example

#include <iostream>
#include <string>
int main() {
 std::string numStr = "9876543210";
 try {
 long long result = std::stoll(numStr);
 std::cout << "Converted value: " << result << std::endl;
 } catch (const std::invalid_argument& e) {
 std::cerr << "Invalid argument: " << e.what() << std::endl;
 } catch (const std::out_of_range& e) {
 std::cerr << "Out of range: " << e.what() << std::endl;
 }
 return 0;
}

Output:

Example Program 2:

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

Example

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
int main() {
 // Open a file containing numeric data in string format
 std::ifstream inputFile("numeric_data.txt");
 if (!inputFile.is_open()) {
 std::cerr << "Error: Unable to open the file." << std::endl;
 return 1; // Return with an error code
 }
 std::vector<std::string> numericStrings;
 std::string currentLine;
 // Read lines from the file and store them in a vector
 while (std::getline(inputFile, currentLine)) {
 numericStrings.push_back(currentLine);
 }
 // Close the file
 inputFile.close();
 // Process the numeric strings and calculate the sum
 long long sum = 0;
 for (const auto& numStr : numericStrings) {
 try {
 long long result = std::stoll(numStr);
 sum += result;
 } catch (const std::invalid_argument& e) {
 std::cerr << "Error: Invalid argument in string '" << numStr << "': " << e.what() << std::endl;
 } catch (const std::out_of_range& e) {
 std::cerr << "Error: Out of range for string '" << numStr << "': " << e.what() << std::endl;
 }
 }
 // Output the sum of converted values
 std::cout << "Sum of converted values: " << sum << std::endl;
 return 0;
}

Output:

Explanation:

  • 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.
  • 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.
  • Closing of File: String the close command, the file is closed once all of its lines have been read.
  • 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).
  • Producing the Total: After that, the total of the converted values is printed to the standard output by the program.
  • 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.
  • 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.
  • String the close command, the file is closed once all of its lines have been read.
  • 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).
  • After that, the total of the converted values is printed to the standard output by the program.

Input Required

This code uses input(). Please provide values below: