p-ranav / tabulate

Table Maker for Modern C++
MIT License
1.94k stars 138 forks source link

Correct table filled area shape method #107

Open lngsv opened 1 year ago

lngsv commented 1 year ago

Hi!

I expect this code to print "1 1"

#include <iostream>
// The header file from single_include
#include "tabulate.hpp"

using namespace tabulate;

int main() {
    Table table;
    table.add_row({"cell"});
    auto [rows, cols] = table.shape();
    std::cout << rows << " " << cols << std::endl;
}

In my environment it prints "8 3".

$ g++ test_tabulate.cpp -o test
$ ./test
8 3

I don't think that matters but I use g++ (GCC) 12.2.0 on CentOS 7

I didn't go deep into details but I believe it prints some numbers that depend on the size of the buffer that is created in TableInternal::shape() method.

Is it possible to fix/add a method that would return an actual number of rows and columns that were added to the table?

This worked for me.

// tabulate::Table
std::pair<size_t, size_t> sizes()
{
    return {rows_, cols_};
}