cout.width 的反转是什么? (C++)

发布于 2024-07-20 09:23:42 字数 286 浏览 4 评论 0原文

我正在尝试 std::cout.width(int) 来看看它做了什么,它将文本向右推以填充最小宽度:

TH

变成:

        TH

填充最小宽度 10。我想知道如果A)有办法扭转这种情况,在文本后面放置一些空格来填充最小宽度,并且B)有没有办法创建最大宽度和最小宽度?

顺便说一句,是否可以创建一个从 coutostream 派生的类?

I was trying std::cout.width(int) to see what it did, and it pushes the text right to fill a minimum width:

TH

becomes:

        TH

to fill a minimum width of 10. I am wondering if A) there is a way to reverse this, have a number of spaces put AFTER the text to fill a minimum width, and B) is there a way to create a maximum width AND a minimum width?

And on a lesser note, is it possible to create a class derived from cout or ostream?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

方觉久 2024-07-27 09:23:42

宽度设置您接下来要使用 cout 打印的内容的“列”大小。

std::cout << left << "Hello";

将在您创建的列中将上面的内容打印为“左”对齐。

默认情况下,不同的“类型”与某些边对齐。

有关此参考页面的更多信息。

Width sets the "column" size for what you are printing next with cout.

std::cout << left << "Hello";

would print the above as "left" aligned in the column you made.

Different "types" are aligned to certain sides by default.

More info on this reference page.

吖咩 2024-07-27 09:23:42

顺便说一句,有可能吗?
创建一个从 cout 派生的类或
流?

一次提出一个问题是个好主意,但您不能从 cout 派生,因为它是一个实例,而不是一个类。 有关派生新输出流类型的详细信息,请阅读本书

And on a lesser note, is it possible
to create a class derived from cout or
ostream?

One question at a time is a good idea, but you can't derive from cout because it is an instance, not a class. For details of deriving a new output stream type, read this book.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文