This example demonstrates how to create a well-formatted table displaying movie information with proper column alignment and header styling.
Complete Example
#include <tabulate/table.hpp>
using namespace tabulate ;
using Row_t = Table :: Row_t ;
int main () {
Table movies;
movies . add_row ( Row_t { "S/N" , "Movie Name" , "Director" , "Estimated Budget" , "Release Date" });
movies . add_row ( Row_t { "tt1979376" , "Toy Story 4" , "Josh Cooley" , "$200,000,000" , "21 June 2019" });
movies . add_row ( Row_t { "tt3263904" , "Sully" , "Clint Eastwood" , "$60,000,000" , "9 September 2016" });
movies . add_row (
{ "tt1535109" , "Captain Phillips" , "Paul Greengrass" , "$55,000,000" , " 11 October 2013" });
// center align 'Director' column
movies . column ( 2 ). format (). font_align ( FontAlign ::center);
// right align 'Estimated Budget' column
movies . column ( 3 ). format (). font_align ( FontAlign ::right);
// right align 'Release Date' column
movies . column ( 4 ). format (). font_align ( FontAlign ::right);
// center-align and color header cells
for ( size_t i = 0 ; i < 5 ; ++ i) {
movies [ 0 ][i]
. format ()
. font_color ( Color ::yellow)
. font_align ( FontAlign ::center)
. font_style ({ FontStyle ::bold});
}
std ::cout << movies << std ::endl;
}
Output
The table will render like this:
┌───────────┬──────────────────┬─────────────────┬──────────────────┬──────────────────┐
│ S/N │ Movie Name │ Director │ Estimated Budget │ Release Date │
├───────────┼──────────────────┼─────────────────┼──────────────────┼──────────────────┤
│ tt1979376 │ Toy Story 4 │ Josh Cooley │ $200,000,000 │ 21 June 2019 │
├───────────┼──────────────────┼─────────────────┼──────────────────┼──────────────────┤
│ tt3263904 │ Sully │ Clint Eastwood │ $60,000,000 │ 9 September 2016 │
├───────────┼──────────────────┼─────────────────┼──────────────────┼──────────────────┤
│ tt1535109 │ Captain Phillips │ Paul Greengrass │ $55,000,000 │ 11 October 2013 │
└───────────┴──────────────────┴─────────────────┴──────────────────┴──────────────────┘
Key Features Demonstrated
Column Alignment
The example shows three different alignment options:
Left Align (Default)
Center Align
Right Align
// S/N and Movie Name columns use default left alignment
// No explicit formatting needed
Right-aligning numeric columns (like budgets) and dates improves readability and follows common table formatting conventions.
Headers are styled differently from data rows to make them stand out:
for ( size_t i = 0 ; i < 5 ; ++ i) {
movies [ 0 ][i]
. format ()
. font_color ( Color ::yellow) // Yellow text color
. font_align ( FontAlign ::center) // Center aligned
. font_style ({ FontStyle ::bold}); // Bold font
}
Row and Cell Access
The example demonstrates two ways to add rows:
// Method 1: Using Row_t type alias
movies . add_row ( Row_t { "S/N" , "Movie Name" , "Director" , "Estimated Budget" , "Release Date" });
// Method 2: Using initializer list directly
movies . add_row ({ "tt1535109" , "Captain Phillips" , "Paul Greengrass" , "$55,000,000" , " 11 October 2013" });
Both methods are equivalent. Use Row_t for clarity or direct initializer lists for brevity.
Applying to Your Data
This pattern works well for any tabular data:
Table employees;
employees . add_row ( Row_t { "ID" , "Name" , "Department" , "Salary" });
employees . add_row ( Row_t { "001" , "Alice Smith" , "Engineering" , "$95,000" });
employees . add_row ( Row_t { "002" , "Bob Johnson" , "Marketing" , "$75,000" });
// Right-align salary column
employees . column ( 3 ). format (). font_align ( FontAlign ::right);
// Style headers
for ( size_t i = 0 ; i < 4 ; ++ i) {
employees [ 0 ][i]
. format ()
. font_color ( Color ::cyan)
. font_align ( FontAlign ::center)
. font_style ({ FontStyle ::bold});
}
Advanced Customization
You can further customize individual cells:
// Highlight a specific movie
movies [ 1 ][ 1 ]. format (). font_color ( Color ::green). font_style ({ FontStyle ::italic});
// Add background color to a budget value
movies [ 1 ][ 3 ]. format (). font_background_color ( Color ::blue);
// Custom border colors for specific cells
movies [ 2 ][ 0 ]. format (). border_color ( Color ::red);
Formatting can be applied at three levels: table-wide, column-wide, or individual cells. Cell-level formatting overrides column-level, which overrides table-level.