揮発性のメモ2

http://d.hatena.ne.jp/iww/

C++でatoi

atoi()を使う

#include <sstream>
#include <iostream>
#include <cstdlib>  // ★← stdlib.hは使わない

int main()
{
  int a;
  string b = "aiueo";

  a = atoi(b.c_str());

  cout << a << endl;
  return 0;
}


stringstreamを使うと、数字じゃないときに0にならないので困る

#include <sstream>
#include <iostream>

int main()
{
  int a;
  string b = "aiueo";

  stringstream ss;
  ss << b;
  ss >> a;   // ★← 何も入らない

  cout << a << endl;  // ★← 未初期化のまま出力されて怖い
  return 0;
}