Seasons.NET

ちょっとした技術ブログです

数値->文字列変換(stringstream)

数値から文字列への変換は、stringstreamを使うと便利です。

#include <sstream>

using namespace std;

int _tmain(int argc, TCHAR* argv[])
{
    std::stringstream ss;
    int num = 10;

    ss << 10;
    cout << ss.str() << endl; //=> 10
    ss << 10;
    cout << ss.str() << endl; //=> 1010
    // ストリームのクリアの仕方
    ss.str(""); // バッファをクリアする。
    ss.clear(stringstream::goodbit);
}