首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >"Permanent“std::setw

"Permanent“std::setw
EN

Stack Overflow用户
提问于 2009-01-01 15:06:50
回答 2查看 13.5K关注 0票数 33

有没有办法永久设置std::setw manipulator (或其函数width)?看看这个:

代码语言:javascript
复制
#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;
}

在运行之后,我看到:

代码语言:javascript
复制
001 2 4 8 10 20 40 80 100

001 002 004 008 010 020 040 080 100

即,除了必须为每个条目设置的setw/width之外,每个操纵器都有自己的位置。与setw一起使用std::copy (或其他东西)有什么优雅的方式吗?我所说的优雅,当然不是指创建自己的函数器或函数来将内容写入std::cout

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2009-01-01 15:39:05

这是不可能的。没有办法让它每次都调用.width。当然,您也可以使用boost:

代码语言:javascript
复制
#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)
        );
}

它确实创建了自己的functor,但它发生在幕后:)

票数 20
EN

Stack Overflow用户

发布于 2014-08-07 09:53:37

由于setwwidth不会产生持久设置,因此一种解决方案是定义一个覆盖operator<<的类型,在值之前应用setw。这将允许该类型的ostream_iteratorstd::copy一起工作,如下所示。

代码语言:javascript
复制
int fieldWidth = 4;
std::copy(v.begin(), v.end(),
    std::ostream_iterator< FixedWidthVal<int,fieldWidth> >(std::cout, ","));

您可以定义:(1) FixedWidthVal作为模板类,带有数据类型(typename)和宽度(值)的参数,以及(2) ostreamoperator<<和对每次插入应用setwFixedWidthVal

代码语言:javascript
复制
// 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循环)来应用它:

代码语言:javascript
复制
// 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;
}

输出()

代码语言:javascript
复制
   1,   2,   4,   8,  16,  32,  64, 128, 256,
   1,   2,   4,   8,  16,  32,  64, 128, 256, 512,1024,2048,4096,
票数 14
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/405039

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档