打印2d表(标题) [英] printing 2d table (headers)

查看:216
本文介绍了打印2d表(标题)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有比这个更好的方法打印2d表格?

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";


推荐答案

您可以将标题放入数组或向量,然后自动生成正确的宽度:

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.

这篇关于打印2d表(标题)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆