C++

Stoll C++

Converting one data type into another is needed for many different purposes. And, if you have some useful built-in functions available, you can easily convert data from one type to another. The C++ programming language offers a list of various built-in functions that can be used for data type conversions. This article is designed to learn one of those conversion functions. Here, we will illustrate the working of the stoll function of the C++ language.

What Is stoll Function in C++ Programing Language?

The stoll function is a C++ built-in function provided by the standard library of C++ to convert a string into a long long int. It takes the input string, computes its long long int value, and returns the computed value. It sets the pointer on the string’s first character, iterating through each character incrementing by one, and reaching the last character.

Syntax of stoll Function

The syntax of the stoll is as follows:

The stoll function takes three parameters: string, index, and base. The input string to be turned into an integer value is contained in the “string” parameter. The “index” parameter is a pointer to an object that is used to track the position of the next character. It can be a null pointer when it is not needed. The base value in which the string will be parsed is stored in the final parameter, called “base”. By default, the base value is 10, and the 0 base value determines the base by the format of the string. It returns the converted integer value in the form of long long int. Let us understand the working of stoll with the examples provided below:

Example 1

The first example of the stoll function will illustrate the basic function of the stoll function. Here, a simple and easy program is provided so that you do not have a problem understanding the function. See the following code and understand the lines of code step-by-step:

#include <bits/stdc++.h>

using namespace std;

int main()

{
    long long int d, h, b;
    string dec = "123456";
    string hex = "FFFFFF";
    string bin = "101010";  
    d = stoll(dec, nullptr, 10);
    h = stoll(hex, nullptr, 16);
    b = stoll(bin, nullptr, 2);  
    cout << dec<<" **Decimal conversion** " << d <<endl<< endl;
    cout <<hex<< " **Hexadecimal conversion** " << h <<endl<<endl;
    cout << bin<<" **Binary conversion** " << b <<endl<<endl;
        return 0;

}

The program starts with the <bits/stdc++.h> header file, which includes all the standard library utilities required for a C++ general program. The “using namespace std;” allows you to include the standard input and output function to perform a certain task. The whole program is written in the main function.

Here, the variables are declared, and values are assigned to them. The strings that need to be transformed are stored in the variables “dec”, “hex”, and “bin”. The d, h, and b variables will hold the converted string in the form of long long int.

Here, the parameters are passed to the stoll function. For converting the decimal string, base 10 is provided. For converting the hexadecimal string, base 16 is provided, and for converting the binary string, base 2 is provided.

Using the cout statement, each string and its converted integer values are printed. The return 0 statement is added at the end of the program to ensure that the execution has been successfully done. It is an optional statement that can be skipped.

Let’s review each converted string in the output provided below:

Example 2

Let us test the stoll function with invalid input. The compiler throws an error or raises an exception if invalid input is provided to the stoll function. If the execution cannot be performed, then the compiler will throw an invalid argument exception. If the conversion cannot be performed due to the out-of-range input value, then the compiler will throw the out-of-range exception. Let us understand this whole scenario with a simple example:

#include <bits/stdc++.h>

using namespace std;

int main() {

    string str = "abcd!~$#@de";
    try{
        cout << stoll(str) << "\n";
    }  
    catch(const std::invalid_argument){
        cerr << "Invalid argument" << "\n";
    }
    return 0;


}

A string containing a special character is provided as an input string to convert using the stoll function. The stoll function cannot convert the special characters of general string data. It raises an invalid argument exception.

The stoll conversion is performed within the “try” block. If the conversion can’t be done, then the “invalid argument” exception in the “catch” block will be raised.

Here is the following output:

In case of an invalid input string, the stoll cannot perform its function and will throw an exception.

Example 3

Now, let us test the stoll function with a mixture of a valid and invalid input string. Here, we will provide a combination of valid and invalid strings to the stoll function to see the working of the stoll function.

#include <bits/stdc++.h>

using namespace std;

int main ()

{

  string str =123456789 0xffff 010;
  string::size_type sz = 0;
  while (!str.empty()) {
    long long ll = stoll (str,&sz,0);
    std::cout << str.substr(0,sz) << “ converted as “ << ll << ‘\n’;
    str = str.substr(sz);
  }
  return 0;


}

The string contains three elements separated by space. We will break each element into one separate string and convert each one of them one by one. The string is iterated through, and each element is converted independently using a “while” loop. The substring() function splits the string into different elements or parts. The base 0 is provided so that the compiler determines the base of conversion automatically by checking the format of input data.

The first element, “123456789”, is a decimal number, so it will be converted to a decimal number using base 10. The decimal conversion of “123456789” should be “123456789”. The second element, “0xffff”, is a mixture of integer, string, and hexadecimal numbers. The stoll function ignores the invalid characters in the string and will compute the conversion of the valid string.

For example, the “0x” in the string will be ignored, and the conversion of “ffff” will be performed. The stoll function will automatically determine the base of conversion from the data format. The “ffff” is an integer in hexadecimal series, so the stoll function will use base 16 for string conversion. And the last element, “010”, is again an integer number, so the stoll function will use 10 to convert the decimal string into a decimal integer. The converted string “010” should be an “010” integer. Let us see the output provided below:

Conclusion

We had a quick overview of the stoll function. With the help of examples, we learned the usage of the stoll function in the C++ programming language. Three unique examples, along with their code explanation, are provided in this guide.

About the author

Omar Farooq

Hello Readers, I am Omar and I have been writing technical articles from last decade. You can check out my writing pieces.