Pretty print a table in C++

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.


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

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


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