Pretty print a table in C++

落爺英雄遲暮 提交于 2020-11-30 05:27:40

问题


I am looking for a library similar to prettytable but in C++

http://code.google.com/p/prettytable/

I know how to generate one myself using either printf or iostream. However, I would like to know if there is a library for this.

I am interested only in writing this ASCII table to the console.

Preferably something like:

std::vector<std::string> headers;
headers.push_back("My Awesome Header 1");
headers.push_back("My Awesome Header 2");
headers.push_back("My Awesome Header 3");


PrettyTablePrinter ptp;
ptp.SetHeaders(headers);
// Set some other options here
ptp.AddRow(data[0]);
ptp.AddRow(data[1]);
ptp.AddRow(data[2]);

ptp.Print(&std::cout);

回答1:


Since I have not found a good C++ solution, I have written one for you all

https://github.com/dattanchu/bprinter/wiki




回答2:


I wasn't satisfied with any of the ones I found online so I wrote my own: https://github.com/friedmud/variadic_table

It uses variadic templates to allow each column to hold a different type. It also only requires C++11.

VariadicTable<std::string, double, int, std::string> vt({"Name", "Weight", "Age", "Brother"});

vt.addRow({"Cody", 180.2, 40, "John"});
vt.addRow({"David", 175.3, 38, "Andrew"});
vt.addRow({"Robert", 140.3, 27, "Fande"});

vt.print();

This will output:

--------------------------------------
| Name |  Weight  |    Age   |Brother|
--------------------------------------
|Cody  |     180.2|        40|John   |
|David |     175.3|        38|Andrew |
|Robert|     140.3|        27|Fande  |
--------------------------------------

This is actively being used in a large software project - so it will be maintained and developed over time. Feel free to submit issues / PRs




回答3:


To my knowledge, you have three major options here :

  • A "C way" through the use of printf with width specifiers
  • A "C++ way" through the use of stream manipulators (in particular std::setw and std::setfill)
  • An intermediate way using Boost.Format which allow you to use printf style formatters with streams.

I'm not aware of any library which could help you in the "table design" more than this.




回答4:


While not exactly what you're looking for, Boost.Spirit contains a library (named Karma) usable to generate this kind of output fairly easily. The docs are here.




回答5:


LibFort

It's Fantastic, they have Several cool table styles too.

https://github.com/seleznevae/libfort




回答6:


It's pretty simple to write an html to create tables in C++, you don't need a library for that. On the other hand if you want table output on console, it can be done, but it's not that easy, especially if you need to do vertical alignment, breaking strings, etc.




回答7:


You may achieve it using ncurses library. Its C library.




回答8:


The most generic way to format any output of all, in fact the only way to do so within the C++ language is with I/O Manipulators.
http://www.fredosaurus.com/notes-cpp/io/omanipulators.html



来源:https://stackoverflow.com/questions/4471714/pretty-print-a-table-in-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!