Hello. I would like to know how to do this beautiful table in the console: table layout

Interested in how to make such indentation in each line, by regular means? There are no shifts, everything is even. Searched, did not find such information. Thank you in advance.

  • Which indent? Which is at the beginning of the line? Or splitting into columns? - Vladimir Martyanov
  • Column break including. The name has a different number of characters, but there are no further shifts. As if the reserve is coming. - SSD

6 answers 6

Two methods are most likely used: Supplementing a line with leading spaces is done through something like printf ("% 4d", 1); Alignment to columns - through tab printing: printf ("1 \ t2 \ n123 \ t2 \ n");

    Use output handlers such as, for example, std::setw , std::left , std::right and others.

    To use them, turn on the <iomanip> header

    Here, for example, how to display the title

     #include <iostream> #include <iomanip> int main() { std::cout << "ID# " << std::setw( 22 ) << std::left << "ATTRIBUTE_NAME" << std::setw( 8 ) << "FLAG" << std::setw( 6 ) << "VALUE" << std::setw( 6 ) << "WORST" << std::setw( 7 ) << "THRESH" << std::setw( 9 ) << "TYPE" << std::setw( 8 ) << "UPDATE" << std::setw( 12 ) << "WHEN_FAILED" << "RAW_VALUE" << std::endl; } 

    As a result of the launch, you will get an output

     ID# ATTRIBUTE_NAME FLAG VALUE WORST THRESH TYPE UPDATE WHEN_FAILED RAW_VALUE 
    • and what, field width (for alignment on the right edge) really can be defined before the longest line which needs to be brought to this field becomes known? how much I lagged behind the progress ... - aleksandr barakin
    • @alexander barakin Tables are designed before they are used in the program. You first determine what the table should look like, what are the lengths of the fields and design the table according to this information. - Vlad from Moscow
    • sorry: I thought that progress had gone forward. - aleksandr barakin

    Try the BPrinter library (based on Boost.Spirit, but can be compiled without it). True, she draws the boundaries of the table.

     #include <bprinter/table_printer.h> using namespace bprinter; TablePrinter tp(&std::cout); tp.AddColumn("Name", 25); tp.AddColumn("Age", 5); tp.AddColumn("Position", 30); tp.AddColumn("Allowance", 9); tp.PrintHeader(); tp << "Dat Chu" << 25 << "Research Assistant" << -0.00000000001337; tp << "John Doe" << 26 << "Too much float" << 125456789.123456789; tp << "John Doe" << 26 << "Typical Int" << 1254; tp << "John Doe" << 26 << "Typical float" << 1254.36; tp << "John Doe" << 26 << "Too much negative" << -125456789.123456789; tp << "John Doe" << 26 << "Exact size int" << 125456789; tp << "John Doe" << 26 << "Exact size int" << -12545678; tp << "John Doe" << 26 << "Exact size float" << -1254567.8; tp << "John Doe" << 26 << "Negative Int" << -1254; tp << "Jane Doe" << bprinter::endl(); tp << "Tom Doe" << 7 << "Student" << -M_PI; tp.PrintFooter(); 

      you need to know the maximum width of the field before the start of the output lines.

      if the width is already known, then everything is simple. for example, the printf() function:

       ### test.cpp #include <stdio.h> int main() { int max_width, value_to_print; max_width = 4; value_to_print = 100; printf("%*d\ttext\n", max_width, value_to_print); value_to_print = 1000; printf("%*d\ttext\n", max_width, value_to_print); } 

      we collect and check:

       $ make test g++ test.cpp -o test $ ./test 100 text 1000 text 

      Help on the printf() function (if installed) can be viewed locally with the command:

       $ man 3 printf 

      adapted from this answer.

      • In C ++, you can write decltype (auto) func () ;. Though cumbersome, but there is a sense. And what's the point of this ad? int void main () :) - Vlad from Moscow
      • int void main is a typo on manual editing. I fixed, thanks. - aleksandr barakin pm
       for(int i=0;i<last;i++) { printf("\tName %5s\t",StudentList[i].name ); printf("\tSurname %5d\t", StudentList[i].surname); printf("\tGroup %5s\t",StudentList[i].group); printf("\tMidel ball %5d\t", StudentList[i].midle); printf("\n\n"); } 

      The best option

        the most universal option: we will need: the структура in which you store the data of each обьекта . there is also a variable структура size in which we will write the length of the longest field according to the following logic: at the start all the values ​​of fields 0 . by recalculating the length of the data values ​​with strlen (if it is not a string then it is necessary to cast the value to the string type via itoa or others). you add all your objects to the list and consider how long each field is. if the length of the field is greater than that in the size structure of the corresponding field, you overwrite it with a new value. at the end you will have the maximum value of each of the fields for output. it remains only to start a cycle in which you put size - strlen(переменная на вывод) + n (where n is the indentation between the columns) spaces and then the text of the variable itself on the output. similarly done if the indent must be done from behind or place the text in the middle.