std::cout 打印字符 N 次

发布于 2024-12-12 07:52:10 字数 168 浏览 0 评论 0原文

如何使用 std::cout 打印一个字符 N 次而不循环?

有没有办法将文本光标移回以消除 std::cout << 的效果std::endl;?即向上移动一行(假设我们在执行 std::cout << std::endl; 操作后从未打印过任何内容)。

How can I print a character N number of times using std::cout without looping?

Is there a way to move the text cursor back to nullify the effect of std::cout << std::endl;? i.e. to move up a line (say we never printed anything after doing the std::cout << std::endl; operation).

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

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

发布评论

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

评论(3

夏九 2024-12-19 07:52:10
 std::cout << std::string(100, '*') << std::endl;

要向上移动一行,您必须求助于终端转义(假设 isatty() 表明您正在运行一个终端)。

 std::cout << std::string(100, '*') << std::endl;

To move a line up, you have to resort to terminal escapes (assuming that isatty() indicates that you are running on one).

追星践月 2024-12-19 07:52:10
std::cout << std::setfill(the_char) << std::setw(100) << "";
std::cout << std::setfill(the_char) << std::setw(100) << "";
与往事干杯 2024-12-19 07:52:10

有没有办法支持我们的方式来消除cout的影响<<结束;
即向上移动一行(假设我们在执行完之后从未打印过任何内容)
计算<<结束;操作)非常感谢!

使用 三元运算符(或者 if 语句,如果您引用的话)...类似...

void PrintCharNtimes(char chatToPrint; int numTimes)
{
   std::cout << std::string(numTimes, chatToPrint) << (numTimes > 0) ? std::endl : ;
}

is there a way to back our way to nullify the effect of cout << endl;
i.e. to move up a line(say we never printed anything after doing the
cout << endl; operation) Thank you so much!

Use the ternary operator (or an if statement if you refer) ... something like ...

void PrintCharNtimes(char chatToPrint; int numTimes)
{
   std::cout << std::string(numTimes, chatToPrint) << (numTimes > 0) ? std::endl : ;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文