根据消息长度打印自定义数量的标头分隔符
假设我想打印:
============
Some message
============
And:
=======================
Other Message long one
=======================
“=”的数量根据消息长度而变化。 打印这类东西最有效的方法是什么?
没有Boost,请只使用STL。
Say I want to print:
============
Some message
============
And:
=======================
Other Message long one
=======================
The number of "=" changes based on the message length. What is the most efficient way to print this sort of a thing?
No boost, just STL please.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您没有指定在这种情况下如何衡量“效率”。 这是一种在必须编写的代码和分配数量方面高效的解决方案:
我可以想象其他替代方案,避免为横幅分配临时字符串,但这些可能会增加实际打印的成本。
You don't specify how you are measuring "efficiency" in this context. Here's one solution that is efficient in terms of code you must write and number of allocations:
I can imagine other alternatives that avoid allocating a temporary string for the banners, but those might come at an increased cost of the actual printing.
iomanip 变体,只是为了好玩。
iomanip variant, just for fun.