格式化表格中的输出,C++

JT *_*ite 8 c++ format console tabular

如何在C++表中将数据输出到控制台?在C#中有一个问题,但我需要它在C++中.

这个,除了在C++中:如何:在控制台应用程序中绘制表格的最佳方式(C#)

Daw*_*son 11

以下是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;
}
Run Code Online (Sandbox Code Playgroud)

您还可以执行其他操作,例如设置浮点数的精度,更改使用setw时用作填充的字符,输出基数10以外的数字等等.

http://cplusplus.com/reference/iostream/manipulators/


Tim*_* S. 5

你不能做一些非常类似于C#例子的事情:

String.Format("|{0,5}|{1,5}|{2,5}|{3,5}|", arg0, arg1, arg2, arg3);
Run Code Online (Sandbox Code Playgroud)

喜欢:

printf("|%5s|%5s|%5s|%5s|", arg0, arg1, arg2, arg3);
Run Code Online (Sandbox Code Playgroud)

以下是我用来做这个的参考:http://www.cplusplus.com/reference/clibrary/cstdio/printf/

  • 对于表格输出,C的printf击败了C++可怕的I/O指针. (2认同)

Yoa*_*ger 5

我找不到我喜欢的东西,所以我做了一个。在https://github.com/haarcuba/text-table找到它

这是其输出的示例:

+------+------+----+
|      |Sex   | Age|
+------+------+----+
|Moses |male  |4556|
+------+------+----+
|Jesus |male  |2016|
+------+------+----+
|Debora|female|3001|
+------+------+----+
|Bob   |male  |  25|
+------+------+----+
Run Code Online (Sandbox Code Playgroud)