stod

stod

double 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.

Parameters

  • str
    String object with the representation of a floating-point number.
  • idx
    Pointer to an object of type size_t, whose value is set by the function to position of the next character in str after the numerical value.
    This parameter can also be a null pointer, in which case it is not used.

Return Value

On success, the function returns the converted floating-point number as a value of type double.

// stod example
#include <iostream>   // std::cout
#include <string>     // std::string, std::stod

int main ()
{
  std::string orbits ("365.24 29.53 1.11");
  std::string::size_type sz;     // alias of size_t
  std::string::size_type sz1;
  double earth = std::stod (orbits,&sz);
  double moon = std::stod (orbits.substr(sz), &sz1);
  double test = std::stod (orbits.substr(sz + sz1));
  std::cout << "earth: " << earth << " moon: " << moon << " test: "<< test <<"\n";
  return 0;
}

运行结果

其它相关


参考

参考

posted @ 2020-11-16 14:56  cyssmile  阅读(290)  评论(0编辑  收藏  举报