最終更新日時(UTC):
が更新

履歴 編集

function
<string>

std::stod(C++11)

namespace std {
  double stod(const std::string& str, std::size_t* idx = nullptr);  // (1)
  double stod(const std::wstring& str, std::size_t* idx = nullptr); // (2)
}

概要

文字列strを数値として読み取って、double型の値に変換する。

効果

パラメータstrstring型であればstd::strtod(str.c_str(), &end)wstringであればstd::wcstod(str.c_str(), &end)を呼び出して、その戻り値を返す。

パラメータidxが非nullptrの場合、変換に使用されなかった要素のインデックス(end - str.c_str())が格納される。

戻り値

変換して得られた数値が返される。

例外

  • 数値への変換が行われなかった場合、std::invalid_argumentが送出される。
  • 以下の条件に合致した場合、std::out_of_rangeが送出される。
    • std::strtoull()関数がerrno変数にERANGEを設定した場合
    • 結果が範囲外の値になった場合 (C++14)

備考

errnoの扱い

  • Visual C++ 11やGCC (libstdc++) 4.8.2では、この関数を呼び出すとerrnoの値が変更される。
  • Clang (libc++) 3.3では、この関数の呼び出し前後でerrnoの値は変化しない。

グローバルロケールの影響

この関数は、setlocale()関数により挙動が変化する。

  • strtod()関数での文字列先頭の空白を読み飛ばす処理に、<cctype>isspace()関数が使用される。
  • 小数点記号はLC_NUMERICで指定されたものが使用される。

#include <iostream>
#include <string>

int main()
{
  // 10進法での変換
  std::cout << "---- decimal point" << std::endl;

  double a = std::stod("1.5"); // std::stod("1.5", nullptr);
  std::cout << a << std::endl;

  double aw = std::stod(L"1."); // std::stod(L"1.", nullptr);
  std::cout << aw << std::endl;

  // 指数表記の変換
  std::cout << "---- base = 8" << std::endl;

  double b = std::stod("0.5e3", nullptr);
  std::cout << b << std::endl;

  double bw = std::stod(L".25e3", nullptr);
  std::cout << bw << std::endl;

  // 16進法での変換
  std::cout << "---- base = 16" << std::endl;

  double c = std::stod("0x1.2P3", nullptr);
  std::cout << c << std::endl;

  double cw = std::stod(L"0x1.2P4", nullptr);
  std::cout << cw << std::endl;

  // 2番目の仮引数の使用例
  std::cout << "---- use of idx parameter" << std::endl;

  std::string es = "30.75%";
  std::size_t ei;
  double e = std::stod(es, &ei);
  std::cout << e << ' ' << es[ei] << std::endl;

  std::wstring ews = L"32%";
  std::size_t ewi;
  double ew = std::stod(ews, &ewi);
  std::cout << ew << ' ' << ewi << std::endl;

  // 文字列先頭に空白がある場合
  std::cout << "---- space character before number" << std::endl;
  std::cout << std::stod("    -1") << std::endl;
  std::cout << std::stod(L"    -.25") << std::endl;
}

出力例

1.5
1
500
250
---- base = 16
9
18
---- use of idx parameter
30.75 %
32 2
---- space character before number
-1
-0.25

実装例

double stod(const std::string& str, std::size_t* idx = nullptr) {
  const char* p = str.c_str();
  char* end;
  errno = 0;
  double x = std::strtod(p, &end);
  if (p == end) {
    throw std::invalid_argument("stod");
  }
  if (errno == ERANGE) {
    throw std::out_of_range("stod");
  }
  if (idx != nullptr) {
    *idx = static_cast<std::size_t>(end - p);
  }
  return x;
}

double stod(const std::wstring& str, std::size_t* idx = nullptr) {
  const wchar_t* p = str.c_str();
  wchar_t* end;
  errno = 0;
  double x = std::wcstod(p, &end);
  if (p == end) {
    throw std::invalid_argument("stod");
  }
  if (errno == ERANGE) {
    throw std::out_of_range("stod");
  }
  if (idx != nullptr) {
    *idx = static_cast<std::size_t>(end - p);
  }
  return x;
}

バージョン

言語

  • C++11

処理系

ただし、Visual C++ 10.0, 11.0は十六進法に対応していない(12.0は未確認)。

関連リンク

C標準ライブラリに由来する関数

  • atof: stodatofstd::stringおよびstd::wstingに対応させたものと見なせる。
  • strtod, wcstod: stodstrtodおよびwcstodをそれぞれstd::stringstd::wstingに対応させたものと見なせる。

ファミリー

  • stoi: 戻り値の型がintとなったもの。
  • stol: 戻り値の型がlongとなったもの。
  • stoll: 戻り値の型がlong longとなったもの。
  • stoul: 戻り値の型がunsigned longとなったもの。
  • stoull: 戻り値の型がunsigned long longとなったもの。
  • stof: 戻り値の型がfloatとなったもの。
  • (stod: この関数自身)
  • stold: 戻り値の型がlong doubleとなったもの。

ロケール依存しない高速な変換関数

参照