9

Should be a trivial question, but found that setw only apply to its immediate following output, and not sure how to allow it apply to all the following output.

For example, for the following line of code

cout<<setw(3)<<setfill('0')<<8<<" "<<9<<endl;

or

cout.width(3);
cout.fill('0');
cout<<8<<" "<<9<<endl;

I want the output to be 008 009 instead of 008 9

4

2 回答 2

7

setw不粘,所以每次都要说:

cout << setfill('0') << setw(3) << 8 << " "
     << setw(3) << 9 << endl;
于 2013-06-30T21:00:16.820 回答
0

唔。为此使用代理结构。

struct setw_all_the_way {
    template <typename T> std::ostring &operator << (T &&t) {
        return std::cout << std::setw(14) << std::forward<T>(t);
    }
};
setw_all_the_way << ...;
于 2019-06-08T17:17:01.690 回答