C++ String stod

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


Syntax of string stod function

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

Parameters of string stod 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 stod function

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


Exceptions for string stod function

The std::stod 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 double type.

Examples for string stod function

Example 1: Converting a Decimal String to Double

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

Program

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

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

Output

The 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.456", representing a decimal number.
  3. The std::stod function converts the string to a double and stores it in the variable num.
  4. The program outputs the 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::stod and the idx parameter to track the position.

Program

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

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

Output

Pi: 3.14159, e: 2.71828

Explanation

  1. The string str contains two floating-point numbers separated by a space.
  2. The std::stod 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 stod

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 {
        double num = std::stod(invalidStr);
    } catch (const std::invalid_argument& e) {
        std::cout << "Invalid argument exception: " << e.what() << std::endl;
    }
    return 0;
}

Output

Invalid argument exception: stod

Explanation

  1. The string invalidStr contains non-numeric characters, making it an invalid input for conversion to a double.
  2. When std::stod 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 double type.

Program

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

int main() {
    std::string largeStr = "1e400"; // A number too large for double
    try {
        double num = std::stod(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: stod

Explanation

  1. The string largeStr contains the value "1e400", which represents a number exceeding the range of the double type.
  2. When std::stod 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.