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
long double stold(const std::string& str, std::size_t* idx = 0);
Parameters of string stold function
| Parameter | Description |
|---|---|
str | The 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 doubletype.
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
#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
- The program includes the necessary headers:
<iostream>for input and output operations, and<string>for string handling. - A string
stris initialized with the value"123.45678901234567890", representing a high-precision decimal number. - The
std::stoldfunction converts the string to along doubleand stores it in the variablenum. - 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
#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
- The string
strcontains two floating-point numbers separated by a space. - The
std::stoldfunction is first called with the original string and a pointer toidx, which stores the position after the first number. - The function is then called again with the substring starting at
idxto extract the second number. - 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
#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
- The string
invalidStrcontains non-numeric characters, making it an invalid input for conversion to a long double. - When
std::stoldis called, it throws astd::invalid_argumentexception because the string does not represent a valid floating-point number. - The exception is caught in the
catchblock, and an appropriate error message is displayed usingstd::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
#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
- The string
largeStrcontains the value"1e5000", which represents a number that exceeds the range of thelong doubletype. - When
std::stoldattempts to convert the string, it detects that the value is out of range and throws astd::out_of_rangeexception. - The exception is caught in the
catchblock, and an appropriate error message is displayed usingstd::cout.
