如何允许setw适用于所有以下stdout? [英] How to allow setw apply to all the following stdout?

查看:155
本文介绍了如何允许setw适用于所有以下stdout?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

应该是一个小问题,但发现setw仅适用于其紧接着的后面的输出,不知道如何允许它应用于以下所有输出。

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;

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

我想输出 008 009 而不是 008 9

推荐答案

/ code>不粘,所以你必须每次都说:

setw isn't sticky, so you have to say it every time:

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

这篇关于如何允许setw适用于所有以下stdout?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆