setw

C++ can setw and setfill pad the end of a string?

一个人想着一个人 提交于 2019-11-29 17:05:02
问题 Is there a way to make setw and setfill pad the end of a string instead of the front? I have a situation where I'm printing something like this. CONSTANT TEXT variablesizeName1 .....:number1 CONSTANT TEXT varsizeName2 ..........:number2 I want to add a variable amount of '.' to the end of "CONSTANT TEXT variablesizeName#" so I can make ":number#" line up on the screen. Note: I have an array of "variablesizeName#" so I know the widest case. Or Should I do it manually by setting setw like this

“Permanent” std::setw

浪尽此生 提交于 2019-11-27 14:56:53
Is there any way how to set std::setw manipulator (or its function width ) permanently? Look at this: #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; } After run, I see: 001 2 4 8 10 20 40 80 100 001 002

“Permanent” std::setw

柔情痞子 提交于 2019-11-26 16:57:55
问题 Is there any way how to set std::setw manipulator (or its function width ) permanently? Look at this: #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: