“Permanent” std::setw

后端 未结 2 1678
轻奢々
轻奢々 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 >(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 
    
    template 
    struct FixedWidthVal
    {
        FixedWidthVal(T v_) : v(v_) {}
        T v;
    };
    
    template 
    std::ostream& operator<< (std::ostream& ostr, const FixedWidthVal &fwv)
    {
        return ostr << std::setw(W) << fwv.v;
    }
    

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

    // fixedWidthTest.cpp
    #include 
    #include 
    #include 
    #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 >(std::cout, ","));
    
        std::cout << std::endl;
    
        // output values computed in loop
        std::ostream_iterator > 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,
    

提交回复
热议问题