在C++中使用cout对齐Integer

Align Integer using cout in C++

本文关键字:cout 对齐 Integer C++      更新时间:2023-10-16

我需要C++使用cout打印:

Header
     1
     2
     3
     4
     5
    10
    11
    12

而不是

Header
    1
    2
    3
    4
    5
    10
    11
    12

我应该如何使用cout格式化这个?

使用IO操纵器setw

#include <iostream>
#include <iomanip>
int main()
{
    std::cout << "Headern";
    for(int i=1; i<13; ++i)
        std::cout << std::setw(6) << i << 'n';
}