“Permanent” std::setw

后端 未结 2 1674
轻奢々
轻奢々 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:30

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

    #include 
    #include 
    #include 
    #include 
    #include 
    
    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 :)

提交回复
热议问题