std::setw
マニピュレータ(またはその機能width
)を永続的に設定する方法はありますか? これを見てください:
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <iterator>
int main( void )
{
int array[] = { 1, 2, 4, 8, 16, 32, 64, 128, 256 };
std::cout.fill( '0' );
std::cout.flags( std::ios::hex );
std::cout.width( 3 );
std::copy( &array[0], &array[9], std::ostream_iterator<int>( std::cout, " " ) );
std::cout << std::endl;
for( int i = 0; i < 9; i++ )
{
std::cout.width( 3 );
std::cout << array[i] << " ";
}
std::cout << std::endl;
}
実行後、次のように表示されます。
001 2 4 8 10 20 40 80 100
001 002 004 008 010 020 040 080 100
つまり、すべてのマニピュレータは、エントリごとに設定する必要があるsetw
/を除いて、その場所を保持します。と一緒に(または他の何かを)width
使用するエレガントな方法はありますか?そして、エレガントとは、 に何かを書き込むための独自のファンクターや関数を作成することを意味するものではありません。std::copy
setw
std::cout