C++ String stof
The std::stof function in C++ converts a string representation of a floating-point number into a float. Introduced in C++11 and defined in the <string> header.
Syntax of string stof function
</>
Copy
float stof(const std::string& str, std::size_t* idx = 0);
Parameters of string stof 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 stof function
On successful conversion, std::stof returns the converted value as a float. If the conversion fails, it throws an exception.
Exceptions for string stof function
The std::stof 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
floattype.
Examples for string stof function
Example 1: Converting a Decimal String to Float
In this example, we convert a string containing a decimal number to a float using std::stof.
Program
</>
Copy
#include <iostream>
#include <string>
int main() {
std::string str = "123.456";
float num = std::stof(str);
std::cout << "The float value is: " << num << std::endl;
return 0;
}
Output
The float 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.456", representing a decimal number. - The
std::stoffunction converts the string to afloatand stores it in the variablenum. - The program outputs the float value using
std::cout.
Example 2: Converting a String with Leading and Trailing Whitespace to Float
In this example, we convert a string with leading and trailing whitespace to a float using std::stof.
Program
</>
Copy
#include <iostream>
#include <string>
int main() {
std::string str = " 789.012 ";
float num = std::stof(str);
std::cout << "The float value is: " << num << std::endl;
return 0;
}
Output
The float value is: 789.012
Explanation
- The string
strcontains the value" 789.012 ", with leading and trailing whitespace. - The
std::stoffunction ignores the whitespace and successfully converts the string to afloat. - The resulting float value is stored in
numand printed usingstd::cout.
Examples for Exception Handling in stof
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 {
float num = std::stof(invalidStr);
} catch (const std::invalid_argument& e) {
std::cout << "Invalid argument exception: " << e.what() << std::endl;
}
return 0;
}
Output
Invalid argument exception: stof
Explanation
- The string
invalidStrcontains non-numeric characters, making it an invalid input for conversion to a float. - When
std::stofis 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 float type.
Program
</>
Copy
#include <iostream>
#include <string>
#include <stdexcept>
int main() {
std::string largeStr = "1e40"; // A number too large for float
try {
float num = std::stof(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: stof
Explanation
- The string
largeStrcontains the value"1e40", which represents a number that exceeds the range of thefloattype. - When
std::stofis called, it throws astd::out_of_rangeexception because the value is too large to be represented as afloat. - The exception is caught in the
catchblock, and an appropriate error message is displayed usingstd::cout.
