如何在C++中将表格中的数据输出到控制台?在 C# 中有一个问题,但我需要在 C++ 中。
这个,C++ 除外:如何:在控制台应用程序中绘制表格的最佳方法 (C#)
以下是 iomanip 的一小部分示例:
#include <iostream>
#include <iomanip>
int main(int argc, char** argv) {
std::cout << std::setw(20) << std::right << "Hi there!" << std::endl;
std::cout << std::setw(20) << std::right << "shorter" << std::endl;
return 0;
}
您还可以执行其他操作,例如设置浮点数的精度、使用 setw 时更改用作填充的字符、以除 10 为基数以外的其他方式输出数字,等等。
我找不到我喜欢的东西,所以我做了一个。在 https://github.com/haarcuba/text-table
找到它这是其输出的示例:
+------+------+----+
| |Sex | Age|
+------+------+----+
|Moses |male |4556|
+------+------+----+
|Jesus |male |2016|
+------+------+----+
|Debora|female|3001|
+------+------+----+
|Bob |male | 25|
+------+------+----+
你不能做一些与 C# 示例非常相似的事情吗:
String.Format("|{0,5}|{1,5}|{2,5}|{3,5}|", arg0, arg1, arg2, arg3);
喜欢:
printf("|%5s|%5s|%5s|%5s|", arg0, arg1, arg2, arg3);
这是我用来制作此内容的参考:https://cplusplus.com/reference/cstdio/printf/
检查列值长度,并记住值的长度以进行格式化。
printf(" %-4s| %-10s| %-5s|\n", "ID", "NAME", "AGE");
看看MySQL shell接口是如何设计的,它会给你一个好主意。