我怎样才能像 table 一样打印出来?

How can I make my print out like a table?

我要打印以下代码:

cout<<current->bookId<<"\t\t"<<current->bookName<<"\t\t"<<current->year<<"\t\t";
cout<<"Checked out by student "<<current->storedBy<<endl;

看起来像这样

BookId          BookName                Year            Status
1000            Machine Learning                1997            Checked out by student 21000000
1200            Data Mining             1991            Checked out by student 21000020
1400            C++ How to Program              2005            Checked out by student 21000020
1500            Pattern Recognition             2000            Checked out by student 21000000

我应该怎么做才能变成这样:

BookId          BookName                        Year            Status
1000            Machine Learning                1997            Checked out by student 21000000
1200            Data Mining                     1991            Checked out by student 21000020
1400            C++ How to Program              2005            Checked out by student 21000020
1500            Pattern Recognition             2000            Checked out by student 21000000

Set field width std::setw 就是您要找的。

用法示例:

#include <iostream>     // std::cout, std::endl
#include <iomanip>      // std::setw

int main () {
  std::cout << std::setw(10) << "test" << std::endl; 
  return 0;
}

由于参数为10,所以std::setw会在屏幕的第10、11、12、13个字符显示"test"

在您的情况下,您将使用与此类似的例程,通过一些试验和错误,您将实现您想要的结果:

std::cout << std::setw(10) << "col1" << std::setw(5) << "col2" << std::endl;