std::stringstream
の と同等の形式でprintf
整数を に出力したい%02d
。これを達成するためのより簡単な方法はありますか:
std::stringstream stream;
stream.setfill('0');
stream.setw(2);
stream << value;
stringstream
(疑似コード) のような形式フラグを にストリーミングすることは可能ですか?
stream << flags("%02d") << value;
std::stringstream
の と同等の形式でprintf
整数を に出力したい%02d
。これを達成するためのより簡単な方法はありますか:
std::stringstream stream;
stream.setfill('0');
stream.setw(2);
stream << value;
stringstream
(疑似コード) のような形式フラグを にストリーミングすることは可能ですか?
stream << flags("%02d") << value;
から標準のマニピュレータを使用できますが、両方を一度に<iomanip>
行うきちんとしたマニピュレータはありません。fill
width
stream << std::setfill('0') << std::setw(2) << value;
ストリームに挿入されたときに両方の機能を実行する独自のオブジェクトを作成することは難しくありません。
stream << myfillandw( '0', 2 ) << value;
例えば
struct myfillandw
{
myfillandw( char f, int w )
: fill(f), width(w) {}
char fill;
int width;
};
std::ostream& operator<<( std::ostream& o, const myfillandw& a )
{
o.fill( a.fill );
o.width( a.width );
return o;
}
使用できます
stream<<setfill('0')<<setw(2)<<value;
標準の C++ では、これ以上のことはできません。または、Boost.Format を使用できます。
stream << boost::format("%|02|")%value;
c-lickプログラミングが使えると思います。
あなたが使用することができますsnprintf
このような
std::stringstream ss;
char data[3] = {0};
snprintf(data,3,"%02d",value);
ss<<data<<std::endl;