如何格式化不同类型大小的字符串(例如 x、xxx、xx、xxx)的二维数组,使其在打印时看起来更像方形
How to format an 2d array of different type sized strings (e.g. x, xxx ,xx ,xxx) to look more square like when printed
这是我的代码 有谁知道我如何格式化它,使它看起来更像一个正方形,而不是因为每个元素中的字符大小不同。
1
int main() {
string a[4][4]= {{"\xC9","\xCD","\xCD","\xBB"},
{"\xBA","p1","p2","\xBA"},
{"\xBA","100","p3","\xBA"},
{"\xC8","\xCD","\xCD","\xBC"}};
printSquare(a);
这是我的代码,但输出看起来很有趣
这里并没有真正的捷径:您必须将它们全部拉到相同的长度。选择哪种长度:硬。您可能首先必须制作所有行,然后找到最长的行,将所有行格式化为具有相同的长度,然后制作顶部和底部栏。
您可以使用标准 C++ 中的 iostreams / stringstream 来做到这一点。但这确实 很痛苦。 iostreams 根本不是一个好的输出格式化库。
我建议使用支持格式字符串的 fmtlib。所以你可以先格式化你所有的行,并保存最长的长度
#include "fmt/format.h"
#include "fmt/ranges.h"
#include <algorithm>
#include <fmt/core.h>
#include <string>
#include <vector>
int main() {
std::vector<std::vector<std::string>> data{
{"p1", "p2"}, {"100", "p3"}, {"very long string"}};
unsigned int longest_row = 0;
for (const auto &row : data) {
unsigned int rowlength =
fmt::formatted_size("{}", fmt::join(row.begin(), row.end(), ""));
longest_row = std::max(longest_row, rowlength);
}
// Explanation: Print corner - empty string - corner, but pad "empty string"
// longest row times with "middle piece" > is for right-aligned padding
// (doesn't matter for empty string)
fmt::print("╔{:═>{}}╗\n", "", longest_row);
for (const auto &row : data) {
// ^ is for centered padding
// fill with " " (we could omit this, but I think it's clearer)
const std::string this_row =
fmt::format("{}", fmt::join(row.begin(), row.end(), ""));
fmt::print("║{: ^{}}║\n", this_row, longest_row);
}
fmt::print("╚{:═>{}}╝\n", "", longest_row);
}
备注:
- 用 std::vectors 替换了您的字符串数组。更方便。
- 不需要使用转义序列在字符串中包含 unicode 字符,除非你的编译器很烂。
- 后来了解了
formatted_size
,事情就简单了
这是我的代码 有谁知道我如何格式化它,使它看起来更像一个正方形,而不是因为每个元素中的字符大小不同。
1
int main() {
string a[4][4]= {{"\xC9","\xCD","\xCD","\xBB"},
{"\xBA","p1","p2","\xBA"},
{"\xBA","100","p3","\xBA"},
{"\xC8","\xCD","\xCD","\xBC"}};
printSquare(a);
这是我的代码,但输出看起来很有趣
这里并没有真正的捷径:您必须将它们全部拉到相同的长度。选择哪种长度:硬。您可能首先必须制作所有行,然后找到最长的行,将所有行格式化为具有相同的长度,然后制作顶部和底部栏。
您可以使用标准 C++ 中的 iostreams / stringstream 来做到这一点。但这确实 很痛苦。 iostreams 根本不是一个好的输出格式化库。
我建议使用支持格式字符串的 fmtlib。所以你可以先格式化你所有的行,并保存最长的长度
#include "fmt/format.h"
#include "fmt/ranges.h"
#include <algorithm>
#include <fmt/core.h>
#include <string>
#include <vector>
int main() {
std::vector<std::vector<std::string>> data{
{"p1", "p2"}, {"100", "p3"}, {"very long string"}};
unsigned int longest_row = 0;
for (const auto &row : data) {
unsigned int rowlength =
fmt::formatted_size("{}", fmt::join(row.begin(), row.end(), ""));
longest_row = std::max(longest_row, rowlength);
}
// Explanation: Print corner - empty string - corner, but pad "empty string"
// longest row times with "middle piece" > is for right-aligned padding
// (doesn't matter for empty string)
fmt::print("╔{:═>{}}╗\n", "", longest_row);
for (const auto &row : data) {
// ^ is for centered padding
// fill with " " (we could omit this, but I think it's clearer)
const std::string this_row =
fmt::format("{}", fmt::join(row.begin(), row.end(), ""));
fmt::print("║{: ^{}}║\n", this_row, longest_row);
}
fmt::print("╚{:═>{}}╝\n", "", longest_row);
}
备注:
- 用 std::vectors 替换了您的字符串数组。更方便。
- 不需要使用转义序列在字符串中包含 unicode 字符,除非你的编译器很烂。
- 后来了解了
formatted_size
,事情就简单了