“Permanent” std::setw

后端 未结 2 1658
轻奢々
轻奢々 2020-12-03 16:39

Is there any way how to set std::setw manipulator (or its function width) permanently? Look at this:

#include 
#inc         


        
相关标签:
2条回答
  • 2020-12-03 17:08

    Since setw and width do not result in a persistent setting, one solution is to define a type that overrides operator<<, applying setw before the value. This would allow an ostream_iterator for that type to function with std::copy as below.

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

    You could define: (1) FixedWidthVal as a template class with parameters for data type (typename) and width (value), and (2) an operator<< for an ostream and a FixedWidthVal that applies setw for each insertion.

    // 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;
    }
    

    Then it could be applied with std::copy (or a for loop):

    // 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;
    }
    

    Output (demo)

       1,   2,   4,   8,  16,  32,  64, 128, 256,
       1,   2,   4,   8,  16,  32,  64, 128, 256, 512,1024,2048,4096,
    
    0 讨论(0)
  • 2020-12-03 17:30

    Well, it's not possible. No way to make it call .width each time again. But you can use boost, of course:

    #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)
            );
    }
    

    It does create its own functor, but it happens behind the scene :)

    0 讨论(0)
提交回复
热议问题