GeezerVison is showing cpprefjp.github.io — std::stod - cpprefjp C++日本語リファレンス
最終更新日時:
2026年10月03日 22時31分04秒 (JST)
Akira Takahashi
が更新
履歴
編集
function
<string>
std:: stod (C++11)
概要
文字列strを数値として読み取って、double型の値に変換する。
効果
パラメータstrがstring型であればstd::strtod(str.c_str(), &end)、wstringであればstd::wcstod(str.c_str(), &end)を呼び出して、その戻り値を返す。
パラメータidxが非nullptrの場合、変換に使用されなかった要素のインデックス(end - str.c_str())が格納される。
戻り値
変換して得られた数値が返される。
例外
備考
errnoの扱い
Visual C++ 11やGCC (libstdc++) 4.8.2では、この関数を呼び出すとerrnoの値が変更される。
Clang (libc++) 3.3では、この関数の呼び出し前後でerrnoの値は変化しない。
グローバルロケールの影響
この関数は、setlocale() 関数により挙動が変化する。
strtod()関数での文字列先頭の空白を読み飛ばす処理に、<cctype>のisspace()関数が使用される。
小数点記号はLC_NUMERIC で指定されたものが使用される。
例
出力例
---- decimal point
1.5
1
---- base = 8
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 ;
}
バージョン
言語
処理系
ただし、Visual C++ 10.0, 11.0は十六進法に対応していない(12.0は未確認)。
関連リンク
C標準ライブラリに由来する関数
atof : stodはatofをstd::stringおよびstd::wstringに対応させたものと見なせる。
strtod , wcstod : stodはstrtodおよびwcstodをそれぞれstd::stringとstd::wstringに対応させたものと見なせる。
ファミリー
stoi : 戻り値の型がintとなったもの。
stol : 戻り値の型がlongとなったもの。
stoll : 戻り値の型がlong longとなったもの。
stoul : 戻り値の型がunsigned longとなったもの。
stoull : 戻り値の型がunsigned long longとなったもの。
stof : 戻り値の型がfloatとなったもの。
(stod: この関数自身)
stold : 戻り値の型がlong doubleとなったもの。
ロケール依存しない高速な変換関数
参照