名前空間
変種
操作

std::stoi, std::stol, std::stoll

From cppreference.com
< cpp‎ | string‎ | basic string
 
 
 
std::basic_string
 
ヘッダ <string> で定義
int       stoi ( const std::string& str,
                 std::size_t* pos = nullptr, int base = 10 );
(1) (C++11以降)
int       stoi ( const std::wstring& str,
                 std::size_t* pos = nullptr, int base = 10 );
(2) (C++11以降)
long      stol ( const std::string& str,
                 std::size_t* pos = nullptr, int base = 10 );
(3) (C++11以降)
long      stol ( const std::wstring& str,
                 std::size_t* pos = nullptr, int base = 10 );
(4) (C++11以降)
long long stoll( const std::string& str,
                 std::size_t* pos = nullptr, int base = 10 );
(5) (C++11以降)
long long stoll( const std::wstring& str,
                 std::size_t* pos = nullptr, int base = 10 );
(6) (C++11以降)

文字列 str の符号付き整数値を解釈します。

char* ptr または wchar_t* ptr(それぞれ (1,3,5) および (2,4,6))という型を持つ、変換関数内部のポインタを考えます。

1) std::strtol(str.c_str(), &ptr, base) を呼び出します。
2) std::wcstol(str.c_str(), &ptr, base) を呼び出します。
3) std::strtol(str.c_str(), &ptr, base) を呼び出します。
4) std::wcstol(str.c_str(), &ptr, base) を呼び出します。
5) std::strtoll(str.c_str(), &ptr, base) を呼び出します。
6) std::wcstoll(str.c_str(), &ptr, base) を呼び出します。

最初の非空白文字が見つかるまで、空白文字(std::isspace を呼び出して識別される)をすべて破棄し、次に有効な基数-n(n は `base`)整数表現を形成するために可能な限り多くの文字を取得し、それを整数値に変換します。有効な整数値は、次の部分で構成されます。

  • (任意)プラスまたはマイナスの記号
  • (オプション) 8進数を表す接頭辞(0)(基数が8または0の場合にのみ適用されます)
  • (オプション) 16進数を表す接頭辞(0xまたは0X)(基数が16または0の場合にのみ適用されます)
  • 数字のシーケンス

baseの有効な値のセットは{0, 2, 3, ..., 36}です。基数2の整数の有効な数字のセットは{0, 1}、基数3の整数は{0, 1, 2}などです。10より大きい基数では、有効な数字にはアルファベット文字が含まれ、基数11の整数ではAaから始まり、基数36の整数ではZzまでとなります。大文字・小文字は区別されません。

現在インストールされているC ロケールによって、追加の数値形式が受け入れられる場合があります。

base の値が 0 の場合、数値基数は自動検出されます。プレフィックスが 0 の場合は 8 進数、プレフィックスが 0x または 0X の場合は 16 進数、それ以外の場合は 10 進数になります。

入力シーケンスにマイナス記号が含まれていた場合、数字のシーケンスから計算された数値は、結果の型における単項マイナス演算子によるかのように否定されます。

pos がヌルポインタでない場合、ptrstr.c_str() 内の最初の変換されていない文字のアドレスを受け取り、その文字のインデックスが計算されて *pos に格納され、変換によって処理された文字数が示されます。

目次

[編集] パラメータ

str - 変換する文字列
pos - 処理された文字数を格納する整数へのアドレス
base - 基数

[編集] 戻り値

str の内容に対応する整数値。

[編集] 例外

[編集]

#include <iomanip>
#include <iostream>
#include <stdexcept>
#include <string>
#include <utility>
 
int main()
{
    const auto data =
    {
        "45",
        "+45",
        " -45",
        "3.14159",
        "31337 with words",
        "words and 2",
        "12345678901",
    };
 
    for (const std::string s : data)
    {
        std::size_t pos{};
        try
        {
            std::cout << "std::stoi(" << std::quoted(s) << "): ";
            const int i{std::stoi(s, &pos)};
            std::cout << i << "; pos: " << pos << '\n';
        }
        catch (std::invalid_argument const& ex)
        {
            std::cout << "std::invalid_argument::what(): " << ex.what() << '\n';
        }
        catch (std::out_of_range const& ex)
        {
            std::cout << "std::out_of_range::what(): " << ex.what() << '\n';
            const long long ll{std::stoll(s, &pos)};
            std::cout << "std::stoll(" << std::quoted(s) << "): " << ll
                      << "; pos: " << pos << '\n';
        }
    }
 
    std::cout << "\nCalling with different radixes:\n";
    for (const auto& [s, base] : {std::pair<const char*, int>
        {"11",  2}, {"22",  3}, {"33",  4}, {"77",  8},
        {"99", 10}, {"FF", 16}, {"jJ", 20}, {"Zz", 36}})
    {
        const int i{std::stoi(s, nullptr, base)};
        std::cout << "std::stoi(" << std::quoted(s)
                  << ", nullptr, " << base << "): " << i << '\n';
    }
}

実行結果の例

std::stoi("45"): 45; pos: 2
std::stoi("+45"): 45; pos: 3
std::stoi(" -45"): -45; pos: 4
std::stoi("3.14159"): 3; pos: 1
std::stoi("31337 with words"): 31337; pos: 5
std::stoi("words and 2"): std::invalid_argument::what(): stoi
std::stoi("12345678901"): std::out_of_range::what(): stoi
std::stoll("12345678901"): 12345678901; pos: 11
 
Calling with different radixes:
std::stoi("11", nullptr, 2): 3
std::stoi("22", nullptr, 3): 8
std::stoi("33", nullptr, 4): 15
std::stoi("77", nullptr, 8): 63
std::stoi("99", nullptr, 10): 99
std::stoi("FF", nullptr, 16): 255
std::stoi("jJ", nullptr, 20): 399
std::stoi("Zz", nullptr, 36): 1295

[編集] 不具合報告

以下の動作変更を伴う欠陥報告が、以前に公開されたC++標準に遡って適用されました。

DR 適用対象 公開された動作 正しい動作
LWG 2009 C++11 std::out_of_range は、以下の場合にスローされません。
std::strtol または std::strtollerrnoERANGE に設定した場合
スローします。

[編集] 関連項目

(C++11)(C++11)
文字列を符号なし整数に変換する
(function) [編集]
(C++11)(C++11)(C++11)
文字列を浮動小数点値に変換する
(function) [編集]
バイト文字列を整数値に変換する
(関数) [編集]
バイト文字列を符号なし整数値に変換する
(関数) [編集]
(C++11)(C++11)
バイト文字列を std::intmax_t または std::uintmax_t に変換する
(関数) [編集]
文字シーケンスを整数値または浮動小数点数値に変換する
(関数) [編集]
バイト文字列を整数値に変換する
(関数) [編集]
(C++11)
整数値または浮動小数点値を string に変換する
(function) [編集]
整数値または浮動小数点値を wstring に変換する
(function) [編集]
English 日本語 中文(简体) 中文(繁體)