Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

printing 2d table (headers)

Tags:

c++

Is there are a better way than this one to print 2d table?

  std::cout 
      << std::setw(25) << left << "FF.name"
      << std::setw(25) << left << "BB.name"
      << std::setw(12) << left << "sw.cycles"
      << std::setw(12) << left << "hw.cycles"  << "\n"
      << std::setw(25) << left << "------"
      << std::setw(25) << left << "------"
      << std::setw(12) << left << "---------"
      << std::setw(12) << left << "---------"  << "\n";
like image 328
name Avatar asked Jun 06 '26 06:06

name


1 Answers

You could put the headers into an array or vector, then generate the correct widths automatically:

boost::array<std::string, 4> head = { ... }

BOOST_FOREACH(std::string& s, head)
{
    int w = 5*(s.length()/5 + 1);
    std::cout << std::setw(w) << left << s;
}
std::cout << '\n';

BOOST_FOREACH(std::string& s, head)
{
    int w = 5*(s.length()/5 + 1);
    std::cout << std::string(w,'-');
}
std::cout << std::endl;

Might be worthwhile if you have lots of headers I guess.

like image 179
Inverse Avatar answered Jun 08 '26 18:06

Inverse



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!