std :: cout打印字符N次

时间:2021-12-06 00:02:01

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

如何在没有循环的情况下使用std :: cout打印字符N次?

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).

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

3 个解决方案

#1


55  

 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).

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

#2


12  

std::cout << std::setfill(the_char) << std::setw(100) << "";

#3


0  

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!

有没有办法支持我们的方法来消除cout << endl的影响;即向前移动一条线(假设我们在做完cout << endl;操作后从未打印过任何东西)非常感谢你!

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

使用三元运算符(如果你引用的话,还是if语句)......类似......

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

#1


55  

 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).

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

#2


12  

std::cout << std::setfill(the_char) << std::setw(100) << "";

#3


0  

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!

有没有办法支持我们的方法来消除cout << endl的影响;即向前移动一条线(假设我们在做完cout << endl;操作后从未打印过任何东西)非常感谢你!

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

使用三元运算符(如果你引用的话,还是if语句)......类似......

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