Open In App

std::stol() and std::stoll() functions in C++

  1. std::stol(): This function converts the string, provided as an argument in the function call, to long int. It parses str interpreting its content as an integral number of the specified base, which is returned as a value of type long int.

    Syntax:

    long int stol (const string&  str, size_t* idx = 0, int base = 10)

    Parameters: The function accepts three parameters which are described as below:

    • str: It specifies a string object with the representation of an integral number.
    • idx : It specifies a 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. The parameter can also be a null pointer, in which case it is not used.
    • base : It specifies the numerical base to determine the number system in which the characters are interpreted. If the base is 0, the base to be used is determined by the format in the sequence. The default value is 10.

    Return Value: The function returns the converted integral number as a value of type long int.




    // CPP code for illustration
    // of stol() function.
    #include <bits/stdc++.h>
    using namespace std;
      
    int main()
    {
      
        // converting decimal number.
        string dec_num = "9876543210";
        cout << "dec_num = " << 
        stol(dec_num, nullptr, 10) << "\n";
      
        // converting hexadecimal number.
        string hex_num = "FFFFFF";
        cout << "hex_num = " << 
        stol(hex_num, nullptr, 16) << "\n";
      
        // converting binary number.
        string binary_num = "1111111";
        cout << "binary_num = " << 
        stol(binary_num, nullptr, 2) << "\n";
      
        return 0;
    }
    
    
    Output:
    dec_num = 9876543210
    hex_num = 16777215
    binary_num = 127
    
  2. std::stoll(): This function converts a string, provided as an argument in the function call, to long long int. It parses str interpreting its content as an integral number of the specified base, which is returned as a value of type long long int.

    Syntax:

    long long int stoll (const string&  str, size_t* idx = 0, int base = 10)
      Parameters: The function accepts three parameters which are described as below:
    • str: This parameter specifies the String object with the representation of an integral number.
    • idx: This parameter specifies the Pointer to an object of type size_t, whose value is set by the function to a position of the next character in str after the numerical value. This parameter can also be a null pointer, in that case, it is not used.
    • base:This parameter specifies the Numerical base to determine the number system in which the characters are interpreted. If the base is 0, the base used by it is determined by the format in the sequence. The default base is 10.

    Return Value: The function returns the converted integral number as a value of type long long int.




    // CPP code for illustration
    // of stoll() function.
    #include <bits/stdc++.h>
    using namespace std;
      
    int main()
    {
      
        // converting decimal number.
        string dec_num = "9876543210";
        cout << "dec_num = " << 
        stoll(dec_num, nullptr, 10) << "\n ";
      
             
        // converting hexadecimal number.
        string hex_num = "FFFFFF";
        cout << "hex_num = " << 
        stoll(hex_num, nullptr, 16) << "\n";
      
        // converting binary number.
        string binary_num = "1111111";
        cout << "binary_num = " <<
        stoll(binary_num, nullptr, 2) << "\n";
      
            return 0;
    }
    
    
    Output:
    dec_num = 9876543210
     hex_num = 16777215
    binary_num = 127
    

Errors and 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 int, either an invalid_argument or an out_of_range exception is thrown.




// CPP code for illustration of stoll()
// function when invalid_argument 
// exception is thrown.
#include <bits/stdc++.h>
using namespace std;
   
int main() {
       
    // An invalid input string that has no
    // integer part.
    string invalid_num = "abcf$#@de";
       
    // stoll() throws invalid_argument exception
    // when conversion process fails.
    try{
        cout << stoll(invalid_num) << "\n";
    }
       
    // catch invalid_argument exception.
    catch(const std::invalid_argument){
        cerr << "Invalid argument" << "\n";
    }
    return 0;
}

Output:

Runtime Errors:
Invalid argument

Article Tags :