function template
<string>
std::stoddouble stod (const string& str, size_t* idx = 0);double stod (const wstring& str, size_t* idx = 0);
Convert string to double
Parses str interpreting its content as a floating-point number, which is returned as a value of type double.If idx is not a null pointer, the function also sets the value of idx to the position of the first character in str after the number.
The function uses strtod (or wcstod) to perform the conversion (see strtod for more details on the process). Note that the format accepted by these functions depends on the current locale.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
// stod example
#include <iostream> // std::cout
#include <string> // std::string, std::stod
int main ()
{
std::string orbits ("365.24 29.53");
std::string::size_type sz; // alias of size_t
double earth = std::stod (orbits,&sz);
double moon = std::stod (orbits.substr(sz));
std::cout << "The moon completes " << (earth/moon) << " orbits per Earth year.\n";
return 0;
}
The moon completes 12.3684 orbits per Earth year.
An invalid idx causes undefined behavior.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4