ゲームが作れるようになるまでがんばる日記

ゲーム制作のことを中心にゲームに関することを書いています

istringstream

C++標準ライブラリにあるistringstreamはatoi()やatof()の代わりに使うことができる。文字列としてstringを使っているならこちらのほうが便利かも。
使い方はistringstreamをstringを引数にして作成し、そこから>>演算子で変換することができる。

#include <iostream>
#include <sstream>
using namespace std;

int main()
{
    string test("123");
    istringstream iss(test);
    int num = 0;
    iss >> num;  // atoi()と同じ
    cout << num;
}

出力結果
123