"永久的" std::setw

34

有没有办法永久设置 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


稍后,类似的问题: https://dev59.com/2FXTa4cB1Zd3GeqP0EdR 和 https://dev59.com/O2w05IYBdhLWcg3wahRN - sancho.s ReinstateMonicaCellio
2个回答

20

很抱歉,这是不可能的。无法让它每次都调用 .width 方法。但当然你可以使用 boost 库。

#include <boost/function_output_iterator.hpp>
#include <boost/lambda/lambda.hpp>
#include <algorithm>
#include <iostream>
#include <iomanip>

int main() {
    using namespace boost::lambda;
    int a[] = { 1, 2, 3, 4 };
    std::copy(a, a + 4, 
        boost::make_function_output_iterator( 
              var(std::cout) << std::setw(3) << _1)
        );
}

它确实创建了自己的函数对象,但这是在幕后发生的 :)


14

由于 setwwidth 不会产生持久性设置,因此一种解决方案是定义一个类型,重写 operator<<,在值之前应用 setw。这将允许该类型的 ostream_iterator 与以下的 std::copy 函数一起使用。

int fieldWidth = 4;
std::copy(v.begin(), v.end(),
    std::ostream_iterator< FixedWidthVal<int,fieldWidth> >(std::cout, ","));

您可以定义:(1) FixedWidthVal 作为一个模板类,具有数据类型(typename)和宽度(value)的参数,以及 (2) 一个 operator<< 用于 ostreamFixedWidthVal,对于每个插入应用 setw
// FixedWidthVal.hpp
#include <iomanip>

template <typename T, int W>
struct FixedWidthVal
{
    FixedWidthVal(T v_) : v(v_) {}
    T v;
};

template <typename T, int W>
std::ostream& operator<< (std::ostream& ostr, const FixedWidthVal<T,W> &fwv)
{
    return ostr << std::setw(W) << fwv.v;
}

然后可以使用 std::copy(或 for 循环)应用:

// fixedWidthTest.cpp
#include <iostream>
#include <algorithm>
#include <iterator>
#include "FixedWidthVal.hpp"

int main () {
    // output array of values
    int array[] = { 1, 2, 4, 8, 16, 32, 64, 128, 256 };

    std::copy(array,array+sizeof(array)/sizeof(int), 
        std::ostream_iterator< FixedWidthVal<int,4> >(std::cout, ","));

    std::cout << std::endl;

    // output values computed in loop
    std::ostream_iterator<FixedWidthVal<int, 4> > osi(std::cout, ",");
    for (int i=1; i<4097; i*=2)
        osi = i; // * and ++ not necessary

    std::cout << std::endl;

    return 0;
}

输出 (演示)

   1,   2,   4,   8,  16,  32,  64, 128, 256,
   1,   2,   4,   8,  16,  32,  64, 128, 256, 512,1024,2048,4096,

2
非常好的设计,我认为它适用于许多情况。如果宽度可以是运行时(而不是编译时)参数,那就太理想了,尽管我无法想出一种好的方法将此信息“输入”到ostream_iterator中。您还可以提供一个方便的函数template<typename T, int W> with_width(T v) { return FixedWidthVal<T>(v, width); }来节省指定类型的时间。 - j_random_hacker
3
嗨,我应该将功劳归于功。我采用了这种方法来自一个代码审查问题,只是添加了数据类型模板参数。很好的建议,方便函数使用。 - chappjc

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接