C++ std::cout 打印字符 N 次
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7897163/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
std::cout to print character N times
提问by shiraz
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;
操作后从未打印任何内容)。
回答by sehe
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()
表明您正在运行一个)。
回答by Benjamin Lindley
std::cout << std::setfill(the_char) << std::setw(100) << "";
回答by Mawg says reinstate Monica
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 : ;
}