C++ String stold

The std::stold function in C++ converts a string representation of a floating-point number into a long double. Introduced in C++11 and defined in the <string> header.


Syntax of string stold function

</>
Copy
long double stold(const std::string& str, std::size_t* idx = 0);

Parameters of string stold function

ParameterDescription
strThe string containing the representation of a floating-point number.
idx (optional)A pointer to a std::size_t object. If provided, it is set to the position of the first character in str after the numerical value. Defaults to 0.

Return Value of string stold function

On successful conversion, std::stold returns the converted value as a long double. If the conversion fails, it throws an exception.


Exceptions for string stold function

The std::stold function can throw the following exceptions:

  • std::invalid_argument: Thrown if the input string does not contain a valid floating-point number.
  • std::out_of_range: Thrown if the converted value exceeds the range of the long double type.

Examples for string stold function

Example 1: Converting a Decimal String to Long Double

In this example, we convert a string containing a decimal number to a long double using std::stold.

Program

</>
Copy
#include <iostream>
#include <string>

int main() {
    std::string str = "123.45678901234567890";
    long double num = std::stold(str);
    std::cout << "The long double value is: " << num << std::endl;
    return 0;
}

Output

The long double value is: 123.456

Explanation

  1. The program includes the necessary headers: <iostream> for input and output operations, and <string> for string handling.
  2. A string str is initialized with the value "123.45678901234567890", representing a high-precision decimal number.
  3. The std::stold function converts the string to a long double and stores it in the variable num.
  4. The program outputs the long double value using std::cout.

Example 2: Extracting Multiple Floating-Point Numbers from a String

In this example, we extract two floating-point numbers from a single string using std::stold and the idx parameter to track the position.

Program

</>
Copy
#include <iostream>
#include <string>

int main() {
    std::string str = "3.1415926535 2.7182818284";
    std::size_t idx;
    long double pi = std::stold(str, &idx);
    long double e = std::stold(str.substr(idx));
    std::cout << "Pi: " << pi << ", e: " << e << std::endl;
    return 0;
}

Output

Pi: 3.1415926535, e: 2.71828

Explanation

  1. The string str contains two floating-point numbers separated by a space.
  2. The std::stold function is first called with the original string and a pointer to idx, which stores the position after the first number.
  3. The function is then called again with the substring starting at idx to extract the second number.
  4. Both numbers are printed using std::cout.

Examples for Exception Handling in stold

Example 1: Handling std::invalid_argument

This exception is thrown when the string does not contain a valid numeric representation.

Program

</>
Copy
#include <iostream>
#include <string>
#include <stdexcept>

int main() {
    std::string invalidStr = "abc123";
    try {
        long double num = std::stold(invalidStr);
    } catch (const std::invalid_argument& e) {
        std::cout << "Invalid argument exception: " << e.what() << std::endl;
    }
    return 0;
}

Output

Invalid argument exception: stold

Explanation

  1. The string invalidStr contains non-numeric characters, making it an invalid input for conversion to a long double.
  2. When std::stold is called, it throws a std::invalid_argument exception because the string does not represent a valid floating-point number.
  3. The exception is caught in the catch block, and an appropriate error message is displayed using std::cout.

Example 2: Handling std::out_of_range

This exception is thrown when the value represented by the string exceeds the range of the long double type.

Program

</>
Copy
#include <iostream>
#include <string>
#include <stdexcept>

int main() {
    std::string largeStr = "1e5000"; // A number too large for long double
    try {
        long double num = std::stold(largeStr);
    } catch (const std::out_of_range& e) {
        std::cout << "Out of range exception: " << e.what() << std::endl;
    }
    return 0;
}

Output

Out of range exception: stold

Explanation

  1. The string largeStr contains the value "1e5000", which represents a number that exceeds the range of the long double type.
  2. When std::stold attempts to convert the string, it detects that the value is out of range and throws a std::out_of_range exception.
  3. The exception is caught in the catch block, and an appropriate error message is displayed using std::cout.