Вопрос

Есть ли способ как установить 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.

Это было полезно?

Решение

Ну, это невозможно.Нет способа заставить его позвонить .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)
        );
}

Это делает создайте свой собственный функтор, но это происходит за сценой :)

Другие советы

С setw и width не приводят к постоянной настройке, одним из решений является определение типа, который переопределяет 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) и ширина (значение), и (2) operator<< для ostream и FixedWidthVal это применимо 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,
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top