如何将“n”个字符的副本写入ostream,就像在python中一样 [英] How to write 'n' copies of a character to ostream like in python
本文介绍了如何将“n”个字符的副本写入ostream,就像在python中一样的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在python中,以下指令: print'a'* 5
将输出 aaaaa
。如何在C ++中结合 std :: ostream
写一个类似的东西,以避免 for
构造?
In python, the following instruction: print 'a'*5
would output aaaaa
. How would one write something similar in C++ in conjunction with std::ostream
s in order to avoid a for
construct?
推荐答案
显而易见的方式是使用 fill_n
:
The obvious way would be with fill_n
:
std::fill_n(std::ostream_iterator<char>(std::cout), 5, 'a');
另一种可能性是只需构造一个字符串:
Another possibility would be be to just construct a string:
std:cout << std::string(5, 'a');
这篇关于如何将“n”个字符的副本写入ostream,就像在python中一样的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文