如何正确地将输出组织到列中?
我想到的第一件事是做一堆 \t,但是如果任何单词比任何其他单词长几个字符,这会导致单词未对齐。
例如,我想要这样的东西:
Name Last Name Middle initial
Bob Jones M
Joe ReallyLongLastName T
相反,通过在我的 cout 语句中仅包含“\t”,我只能设法获得
Name Last Name Middle initial
Bob Jones M
Joe ReallyLongLastName T
或
Name Last Name Middle initial
Bob Jones M
Joe ReallyLongLastName T
我还需要做什么?
编辑:所以我知道我应该首先计算我想要显示的每列的最大宽度,然后相应地添加填充空间。但是我可以如何以及使用什么功能来做到这一点呢?我应该简单地计算字符串中的字符数,然后从那里开始吗?
The first thing that comes to my mind is to do a bunch of \t's, but that would cause words to be misaligned if any word is longer than any other word by a few characters.
For example, I would like to have something like:
Name Last Name Middle initial
Bob Jones M
Joe ReallyLongLastName T
Instead, by including only "\t"'s in my cout statement I can only manage to get
Name Last Name Middle initial
Bob Jones M
Joe ReallyLongLastName T
or
Name Last Name Middle initial
Bob Jones M
Joe ReallyLongLastName T
What else would I need to do?
EDIT: So I get that I should first count the maximum width of each column I want displayed, and then adding padding spaces accordingly. But how, and with what functions, can I go about doing this? Should I simply count the number of chars in a string and then go from there?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
使用
std::setw
中的std::setw
>例如
输出:
Use
std::setw
from<iomanip>
e.g.
Output:
使用
printf()
填充以及 minus 标志进行左对齐,这会产生:
Use
printf()
padding with the minus flag for left-alignementWhich produces:
您还应该考虑到不同的编辑器/查看器显示具有不同选项卡宽度的文本。因此,使用选项卡时,在一个查看器中看起来排列良好的文本在另一个查看器中可能看起来很丑。
如果您确实想产生良好的排列,则可以使用填充空格,并且可以对文本执行两次传递:首先计算每列的最大宽度,然后向每列添加适量的填充空格。对于后者,您还可以使用定制的
printf
调用。更新:计算列宽基本上意味着计算给定列中字符串的长度。可以使用
string::length()
或strlen()
来完成,具体取决于您是否使用std::string
或char*
(推荐前者)。然后,您只需迭代该列中的所有单词,比较到目前为止的最大单词长度,如果当前单词更长,则将该长度设置为新的最大值。如果您将单词存储在 STL 容器中,您甚至可以使用std::max_element< /code> 算法
通过单个函数调用为您完成这项工作。
You should also take into account that different editors/viewers show text with different tab width. So using tabs, text which looks nicely arranged in one viewer may look ugly in another.
If you really want to produce nice arrangement, you could use padding spaces, and you could do two passes on your text: first count the maximum width of each column, then add the right amount of padding spaces to each column. For the latter, you could also use a tailor made
printf
call.Update: Counting the column width basically means counting the length of strings you have in given column. It can be done using
string::length()
orstrlen()
, depending on whether you are usingstd::string
orchar*
(the former is recommended). Then you just iterate through all the words in that column, compare the max word length you have so far, and if the current word is longer, you set that length to be the new max. If you store your words in an STL container, you can even use thestd::max_element
algorithm to do the job for you with a single function call.对于这种情况,通常需要两次通过:一次发现每列的最大宽度,另一次进行打印。对于标准 iostream,您可以使用
width()
例程让 iostream 自动为您处理填充。For situations like this typically two passes are required: one to discover the max width of each column and another to do the printing. For standard iostreams you can use the
width()
routine to have the iostream handle the padding for you automatically.使用字符串格式(来自 stdio)来显示每一行。
http://www.cppreference.com/wiki/c/io/printf
它可以让您设置最小字段宽度,因此它将为您填充每个字段的其余部分。
Use string formatting (from stdio) to display each line.
http://www.cppreference.com/wiki/c/io/printf
It'll let you set minimum field widths and so it will pad the rest of each field for you.