Overview
The Table class is the main entry point for creating tables in Tabulate. It provides methods for adding rows, accessing cells, formatting, and printing tables.
Constructor
Creates a new empty table.
Type Definitions
Row_t
using Row_t = std::vector<variant<std::string, const char *, string_view, Table>>;
Represents a row of data that can contain strings, string views, or nested tables.
Methods
add_row
Table& add_row(const Row_t& cells)
Adds a new row to the table.
Vector of cell values. Each cell can be a string, const char*, string_view, or a nested Table.
Returns: Reference to the table for method chaining.
Example:
using namespace tabulate;
Table table;
table.add_row({"Name", "Age", "City"});
table.add_row({"Alice", "25", "New York"});
operator[]
Row& operator[](size_t index)
Accesses a row by index using bracket notation.
Returns: Reference to the row at the specified index.
Example:
table.add_row({"Header 1", "Header 2"});
table[0].format().font_color(Color::blue);
row
Accesses a row by index.
Returns: Reference to the row at the specified index.
column
Column column(size_t index)
Accesses a column by index.
Returns: Column object representing the specified column.
Example:
table.column(0).format().width(20).font_align(FontAlign::center);
Accesses the table’s format settings.
Returns: Reference to the table’s Format object.
Example:
table.format()
.border_color(Color::yellow)
.font_color(Color::white)
.corner("+");
print
void print(std::ostream& stream)
Prints the table to an output stream.
Output stream to write the table to.
Example:
str
Converts the table to a string representation.
Returns: String containing the formatted table.
Example:
std::string table_output = table.str();
size
Returns the number of rows in the table.
Returns: Number of rows.
shape
std::pair<size_t, size_t> shape()
Returns the dimensions of the table.
Returns: Pair containing (number of rows, number of columns).
Example:
auto [rows, cols] = table.shape();
std::cout << "Table has " << rows << " rows and " << cols << " columns" << std::endl;
Iterators
begin / end
auto begin() -> RowIterator
auto end() -> RowIterator
Provides iteration over rows in the table.
Example:
for (auto& row : table) {
row.format().padding(1);
}
Stream Operator
std::ostream& operator<<(std::ostream& stream, const Table& table)
Allows printing tables directly to output streams.
Example:
Table table;
table.add_row({"Hello", "World"});
std::cout << table << std::endl;
Complete Example
#include <tabulate/table.hpp>
using namespace tabulate;
int main() {
Table employees;
// Add header row
employees.add_row({"Name", "Age", "Department", "Salary"});
employees[0].format()
.font_color(Color::white)
.font_align(FontAlign::center)
.font_style({FontStyle::bold});
// Add data rows
employees.add_row({"Alice Johnson", "28", "Engineering", "$95,000"});
employees.add_row({"Bob Smith", "35", "Marketing", "$80,000"});
employees.add_row({"Carol Davis", "42", "Management", "$120,000"});
// Format columns
employees.column(0).format().width(20);
employees.column(1).format().width(8).font_align(FontAlign::center);
employees.column(2).format().width(15);
employees.column(3).format().width(12).font_align(FontAlign::right);
// Format entire table
employees.format()
.border_color(Color::blue)
.corner("+")
.border_top("-")
.border_bottom("-")
.border_left("|")
.border_right("|");
std::cout << employees << std::endl;
return 0;
}