如何正确地将输出组织到列中?

发布于 2024-08-25 16:41:51 字数 639 浏览 1 评论 0原文

我想到的第一件事是做一堆 \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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

我的奇迹 2024-09-01 16:41:51

使用 std::setw 中的 std::setw >

例如

using std::cout;
using std::setw;

cout << setw(10) << "This" <<
        setw(10) << "is" <<
        setw(10) << "a" <<
        setw(10) << "test" << '\n';

输出:

      This        is         a      test

Use std::setw from <iomanip>

e.g.

using std::cout;
using std::setw;

cout << setw(10) << "This" <<
        setw(10) << "is" <<
        setw(10) << "a" <<
        setw(10) << "test" << '\n';

Output:

      This        is         a      test
酷遇一生 2024-09-01 16:41:51

使用 printf() 填充以及 minus 标志进行左对齐,

 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", "");

这会产生:

Name    Last Name            Middle initial
Bob     Jones                M
Joe     ReallyLongLastName   T

Use printf() padding with the minus flag for left-alignement

 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
爱你是孤单的心事 2024-09-01 16:41:51

您还应该考虑到不同的编辑器/查看器显示具有不同选项卡宽度的文本。因此,使用选项卡时,在一个查看器中看起来排列良好的文本在另一个查看器中可能看起来很丑。

如果您确实想产生良好的排列,则可以使用填充空格,并且可以对文本执行两次传递:首先计算每列的最大宽度,然后向每列添加适量的填充空格。对于后者,您还可以使用定制的 printf 调用。

更新:计算列宽基本上意味着计算给定列中字符串的长度。可以使用 string::length()strlen() 来完成,具体取决于您是否使用 std::stringchar* (推荐前者)。然后,您只需迭代该列中的所有单词,比较到目前为止的最大单词长度,如果当前单词更长,则将该长度设置为新的最大值。如果您将单词存储在 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() 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 algorithm to do the job for you with a single function call.

请你别敷衍 2024-09-01 16:41:51

对于这种情况,通常需要两次通过:一次发现每列的最大宽度,另一次进行打印。对于标准 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.

哑剧 2024-09-01 16:41:51

使用字符串格式(来自 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文