GeezerVison is showing cpprefjp.github.io — std::stoull - cpprefjp C++日本語リファレンス
最終更新日時:
2026年10月03日 22時31分04秒 (JST)
Akira Takahashi
が更新
履歴
編集
function
<string>
std:: stoull (C++11)
namespace std {
unsigned long long stoull ( const std::string & str ,
std::size_t * idx = nullptr ,
unsigned long long base = 10 ); // (1)
unsigned long long stoull ( const std::wstring & str ,
std::size_t * idx = nullptr ,
unsigned long long base = 10 ); // (2)
}
概要
文字列strを数値として読み取って、unsigned long long型の値に変換する。
効果
パラメータstrがstring型であればstd::strtoull(str.c_str(), &end, base)、wstring型であればstd::wcstoull(str.c_str(), &end, base)を呼び出して、その戻り値を返す。
パラメータidxが非nullptrの場合、変換に使用されなかった要素のインデックス(end - str.c_str())が格納される。
パラメータbaseは、整数文字列strの基数を表す。デフォルトでは10進数として文字列を整数に変換する。基数は2から36(36含む)進数を指定できる。基数を0とした場合は、文字列のプレフィックスから基数が自動的に選択される。自動的な選択のルールは、以下のようになる:
先頭が0:8進数
先頭が0xもしくは0X:16進数
戻り値
変換して得られた数値が返される。
例外
備考
errnoの扱い
Visual C++ 11やGCC (libstdc++) 4.8.2では、この関数を呼び出すとerrnoの値が変更される。
Clang (libc++) 3.3では、この関数の呼び出し前後でerrnoの値は変化しない。
グローバルロケールの影響
この関数は、setlocale() 関数により挙動が変化する。
strtoull()関数での文字列先頭の空白を読み飛ばす処理に、<cctype>のisspace()関数が使用されるためである。
例
#include <iostream>
#include <string>
int main ()
{
// 10進法での変換
{
std::cout << "---- base = 10" << std::endl ;
unsigned long long x = std::stoull ( "10" ); // std::stoull ("10", nullptr, 10);
std::cout << x << std::endl ;
unsigned long long xw = std::stoull ( L "11" ); // std::stoull (L"11", nullptr, 10);
std::cout << xw << std::endl ;
}
// 2進法での変換
{
std::cout << "---- base = 2" << std::endl ;
unsigned long long x = std::stoull ( "1001" , nullptr , 2 );
std::cout << x << std::endl ;
unsigned long long xw = std::stoull ( L "01001" , nullptr , 2 ); // 先頭に0が付いていてもよい
std::cout << xw << std::endl ;
}
// 8進法での変換
{
std::cout << "---- base = 8" << std::endl ;
unsigned long long x = std::stoull ( "10" , nullptr , 8 );
std::cout << x << std::endl ;
unsigned long long xw = std::stoull ( L "11" , nullptr , 8 );
std::cout << xw << std::endl ;
}
// 16進法での変換
{
std::cout << "---- base = 16" << std::endl ;
unsigned long long x = std::stoull ( "10" , nullptr , 16 );
std::cout << x << std::endl ;
unsigned long long xw = std::stoull ( L "11" , nullptr , 16 );
std::cout << xw << std::endl ;
}
// 16進法での変換(プレフィックス付き)
{
unsigned long long x = std::stoull ( "0x20" , nullptr , 16 );
std::cout << x << std::endl ;
unsigned long long xw = std::stoull ( L "0x21" , nullptr , 16 );
std::cout << xw << std::endl ;
}
// base = 0による10進法・8進法・16進法の自動判別
{
std::cout << "---- base = 0" << std::endl ;
std::cout << std::stoull ( "100" , nullptr , 0 ) << std::endl ;
std::cout << std::stoull ( "0100" , nullptr , 0 ) << std::endl ;
std::cout << std::stoull ( "0x100" , nullptr , 0 ) << std::endl ;
std::cout << std::stoull ( L "100" , nullptr , 0 ) << std::endl ;
std::cout << std::stoull ( L "0100" , nullptr , 0 ) << std::endl ;
std::cout << std::stoull ( L "0x100" , nullptr , 0 ) << std::endl ;
}
// 2番目の仮引数の使用例
{
std::cout << "---- use of idx parameter" << std::endl ;
std::string s = "30%" ;
std::size_t i ;
unsigned long long x = std::stoull ( s , & i );
std::cout << x << ' ' << s [ i ] << std::endl ;
std::wstring ws = L "31%" ;
std::size_t wi ;
unsigned long long xw = std::stoull ( ws , & wi );
std::cout << xw << ' ' << wi << std::endl ;
}
// 文字列先頭に空白がある場合
{
std::cout << "---- space character before number" << std::endl ;
std::cout << std::stoull ( " 1" ) << std::endl ;
std::cout << std::stoull ( L " 2" ) << std::endl ;
}
}
出力
---- base = 10
10
11
---- base = 2
9
9
---- base = 8
8
9
---- base = 16
16
17
32
33
---- base = 0
100
64
256
100
64
256
---- use of idx parameter
30 %
31 2
---- space character before number
1
2
実装例
バージョン
言語
処理系
関連リンク
C標準ライブラリに由来する関数
atoll: stoullはatollをstd::stringおよびstd::wstringに対応させ、戻り値の型をunsigned long longに変更したものと見なせる。
strtoll, wcstoll: stoullはstrtollおよびwcstoullをそれぞれstd::stringとstd::wstringに対応させたものと見なせる。
ファミリー
stoi : 戻り値の型がintとなったもの。
stol : 戻り値の型がlongとなったもの。
stoll : 戻り値の型がlong longとなったもの。
stoul : 戻り値の型がunsigned longとなったもの。
(stoull: この関数自身)
stof : 戻り値の型がfloatとなったもの。
stod : 戻り値の型がdoubleとなったもの。
stold : 戻り値の型がlong doubleとなったもの。
ロケール依存しない高速な変換関数
参照