C++ 如何将输出正确组织到列中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2436004/
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
How do I correctly organize output into columns?
提问by wrongusername
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.
我想到的第一件事是做一堆\t,但是如果任何单词比任何其他单词长几个字符,这会导致单词未对齐。
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
相反,通过在我的 cout 语句中只包含“\t”,我只能设法得到
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?
编辑:所以我知道我应该首先计算我想要显示的每列的最大宽度,然后相应地添加填充空间。但是,我可以如何以及使用哪些功能来执行此操作?我应该简单地计算字符串中的字符数然后从那里开始吗?
回答by Peter Alexander
回答by Péter T?r?k
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.
如果你真的想产生漂亮的排列,你可以使用填充空格,你可以对文本进行两次传递:首先计算每列的最大宽度,然后为每列添加适量的填充空格。对于后者,您还可以使用量身定制的printf
电话。
Update:Counting the column width basically means counting the length of strings you have in given column. It can be done using string::length()
or strlen()
, depending on whether you are using std::string
or char*
(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 the std::max_element
algorithmto do the job for you with a single function call.
更新:计算列宽基本上意味着计算给定列中字符串的长度。可以使用string::length()
或完成strlen()
,具体取决于您是否使用std::string
或char*
(推荐使用前者)。然后您只需遍历该列中的所有单词,比较迄今为止的最大单词长度,如果当前单词更长,则将该长度设置为新的最大值。如果您将单词存储在 STL 容器中,您甚至可以使用该std::max_element
算法通过单个函数调用为您完成这项工作。
回答by Alex Jasmin
Use printf()
padding with the minusflag for left-alignement
使用printf()
带有减号标志的填充进行左对齐
printf("%-8s%-21s%-7s%-6s\n", "Name", "Last Name", "Middle", "initial");
printf("%-8s%-21s%-7s%-6s\n", "Bob", "Jones", "M", "");
printf("%-8s%-21s%-7s%-6s\n", "Joe", "ReallyLongLastName", "T", "");
Which produces:
其中产生:
Name Last Name Middle initial
Bob Jones M
Joe ReallyLongLastName T
回答by fbrereto
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.
对于这种情况,通常需要两次通过:一次发现每列的最大宽度,另一次进行打印。对于标准 iostream,您可以使用width()
例程让 iostream 自动为您处理填充。
回答by kafuchau
Use string formatting (from stdio) to display each line.
使用字符串格式(来自 stdio)来显示每一行。
http://www.cppreference.com/wiki/c/io/printf
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.
它将让您设置最小字段宽度,因此它将为您填充每个字段的其余部分。