操纵标准输出流以水平打印多行字符串
因此,我有3个字符串,这些弦应该占据3行。我认为这是代表我的弦的好方法:
std::string str1 = "███████\n███1███\n███████";
std::string str2 = "███████\n███2███\n███████";
std::string str3 = "███████\n███3███\n███████";
但是我意识到,当我这样做并只想说琴弦时,它们会彼此印在我不想要的彼此上。我希望输出看起来像这样:
█████████████████████
███1██████2██████3███
█████████████████████
如何实现这种效果?我只知道设置要操纵输出,但是我不知道在这里有什么帮助。
注意:我将将这些存储在数组中,而不是在数组上循环并打印它们,我觉得这可能也可以改变解决方案。
So I have three strings and these strings are supposed to occupy 3 lines. I thought this was a good way to represent my string:
std::string str1 = "███████\n███1███\n███████";
std::string str2 = "███████\n███2███\n███████";
std::string str3 = "███████\n███3███\n███████";
But I realise that when I do this and just cout the strings, they get printed on top of each other which is not I want. I want the output to look like this:
█████████████████████
███1██████2██████3███
█████████████████████
How can I achieve this effect? I only know setw to manipulate the output however I don't know how that could help here.
note: I will have these stored in an array and than loop over the array and print them, I feel like that might change the solution a bit as well.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您确定您的输出将始终显示在现代终端上,以支持“ “ nofollow noreferrer”>然后,您可以利用它来发挥自己的优势。
这会产生输出:
当然,间距的添加是完全可选的,仅是出于示范目的而添加的。
优点:UTF-8和漂亮的颜色!
这种方法的优点是,您不必为包含多字节字符的字符串(UTF-8,如您所做的)或其他任何其他信息(例如终端颜色序列< /strong> 。
也就是说,您可以通过将颜色序列添加到每个
strn
变量来对每个块的颜色不同! (警告是您必须在每个新线上重复一个颜色序列。 >这是各种终端的已知问题... )相对与绝对的看法,将
另一个警告定位到此特殊示例是您必须意识到文本caret的位置(“光标”)每次输出后结束。您也可以使用终端逃生序列在每个输出之前绝对将其定位。
那么,您就不必关心脑袋最终到达的位置。只需在打印之前指定绝对位置即可。只是不要让文字滚动!
Windows OS注意事项
最终,如果您在Windows上并使用旧的Windows控制台,则您必须 必须 初始化ANSI终端序列的终端和 utf的终端-8输出:
这对新的Windows终端没有损害。我建议您这样做,只是因为您不知道用户将用来运行您的程序中的哪一个中的哪一个。
If you are certain that your output will always be displayed on a modern terminal supporting “ANSI Escape Codes” then you can use that to your advantage.
This produces the output:
The addition of spacing is, of course, entirely optional and only added for demonstrative purposes.
Advantages: UTF-8 and Pretty colors!
The advantage to this method is that you do not have to store or otherwise take any special care for strings containing multi-byte characters (UTF-8, as yours does) or any additional information like terminal color sequences.
That is, you could color each of your blocks differently by adding a color sequence to each
strN
variable! (The caveat is that you must repeat a color sequence after every newline. This is a known problem with various terminals...)Relative vs Absolute Caret Positioning
The other caveat to this particular example is that you must be aware of where the text caret (“cursor”) ends-up after each output. You could also use terminal escape sequences to absolutely position the caret before every output.
Then you wouldn’t have to care where the caret ends up. Just specify an absolute position before printing. Just don’t let the text scroll!
Windows OS Considerations
Finally, if you are on Windows and using the old Windows Console, you must initialize the terminal for ANSI terminal sequences and for UTF-8 output:
This does no harm to the new Windows Terminal. I recommend you do it either way just because you do not know which of the two your user will use to run your program, alas.
将每个卡的行作为元素存储在数组中。这很容易。
输出:
同样,如果需要的话,在这些空间之间添加一个空间非常容易。
Store the rows of each card as elements in an array. That makes it pretty easy.
Output:
Again, pretty easy to add a space between those, if you want.
您可以在
\ n
上将每个分割并打印它们。我们可以使用std :: stringstream
用于通过\ n
拆分。输出:
链接到demo
You could split each on
\n
and print them. We can usestd::stringstream
for splitting by\n
.Output:
Link to Demo