code_junkie

“Permanent” std::setw

Nov 13th, 2011
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.82 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <algorithm>
  4. #include <iterator>
  5.  
  6. int main( void )
  7. {
  8. int array[] = { 1, 2, 4, 8, 16, 32, 64, 128, 256 };
  9. std::cout.fill( '0' );
  10. std::cout.flags( std::ios::hex );
  11. std::cout.width( 3 );
  12.  
  13. std::copy( &array[0], &array[9], std::ostream_iterator<int>( std::cout, " " ) );
  14.  
  15. std::cout << std::endl;
  16.  
  17. for( int i = 0; i < 9; i++ )
  18. {
  19. std::cout.width( 3 );
  20. std::cout << array[i] << " ";
  21. }
  22. std::cout << std::endl;
  23. }
  24.  
  25. #include <boost/function_output_iterator.hpp>
  26. #include <boost/lambda/lambda.hpp>
  27. #include <algorithm>
  28. #include <iostream>
  29. #include <iomanip>
  30.  
  31. int main() {
  32. using namespace boost::lambda;
  33. int a[] = { 1, 2, 3, 4 };
  34. std::copy(a, a + 4,
  35. boost::make_function_output_iterator(
  36. var(std::cout) << std::setw(3) << _1)
  37. );
  38. }
Add Comment
Please, Sign In to add comment