C++更改“cout”的输出

发布于 2024-10-07 00:05:28 字数 60 浏览 0 评论 0原文

是否可以更改用“cout”打印的文本?我想让它显示某些内容的当前百分比,而不必为每个百分比换行。这可能吗?

Is it possible to change text printed with "cout"? I would like to make it show the current percentage of something without having to have a new line for each percentage. Is this possible?

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

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

发布评论

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

评论(3

゛时过境迁 2024-10-14 00:05:28

这对我有用:

std::cout << "1111";
std::cout << "\r";
std::cout << "2222";

\r 是回车符。将“光标”放回行首。

或者,您可以使用 \b 字符。这是退格键。打印时会返回一个字符。

This works for me:

std::cout << "1111";
std::cout << "\r";
std::cout << "2222";

\r is a carriage return symbol. Puts the "cursor" back to the beginning of the line.

Alternatively you can use \b character. This is backspace. When printed it goes one character back.

那伤。 2024-10-14 00:05:28

一般来说这是不可能的。 (想象一下,cout 的输出直接送入打印机。您如何指示它“取消打印”最后一行?)cout 是一个输出流,它使没有关于输出发送到哪个介质或该介质的功能的假设。在某些情况下,特定的技巧可以达到你想要的效果,但在其他情况下却会严重失败。如果您想要比直接输出纯文本更动态的东西,也许 cout 不是合适的工具。

In general it is not possible. (imagine that the output from cout is fed directly to a printer. How would you instruct it to "unprint" the last line?) cout is an output stream, it makes no assumptions about which medium the output is sent to, or about the capabilities of that medium. Specific tricks can achieve what you want in some cases, but will fail horribly in others. If you want anything more dynamic than straight output of plain text, perhaps cout isn't the right tool to use.

云雾 2024-10-14 00:05:28

您绝对不会从 cout 得到的一件事是终端线长度。由于可以更改此设置,因此您可能会使用太长的行,这(使用“\r”)将导致每次更新时打印新行。如果您想使用特定平台,则使用特定于平台的函数来获取终端大小(请注意,您可能根本不附加到任何终端,例如重定向到文件)。

One thing that you will definitely not get from cout is terminal line length. As this can be changed, you might use too long lines, which (using '\r') will cause printing new lines every update. If you want to use specific platform then use platform-specific functions to obtain terminal size (mind that you might not be attached to any terminal at all, eg. redirected to file).

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