C++ 使用setw()正确格式化输出

C++ 使用setw()正确格式化输出,c++,C++,还有别的方法吗?如果此代码的第节使其工作,但是否有任何方法可以使用setw()正确组织(*) void showTheater(char theater[][20],int row,int seat) { cout << "Seats: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19" << endl; for (int i = 0; i <= row; i++) {

还有别的方法吗?如果此代码的第节使其工作,但是否有任何方法可以使用setw()正确组织(*)

void showTheater(char theater[][20],int row,int seat)
{
    cout << "Seats:  0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19" << endl;
    for (int i = 0; i <= row; i++)
    {

        for (int j = 0; j <= seat; j++)
        {
            if (j == 0) {
                cout << "Row " << i;
            }
            else if (i < 10) {
                cout << setw(3) << theater[row][seat];
            }
            else {
                cout <<" " << theater[row][seat]<<" ";
            }

        }

        cout << "\n";
    }
}
带有if部分的输出:


关键是使用
std::setw
(与
std::left
)作为行的索引。这是代码。请注意,现在星星也与标题对齐。在发布的代码中,
剧院
的索引也有错误

void showTheater(char theater[][20], int row, int seat) {
    std::vector<int> Seats (seat);
    for (int i = 0; i < seat; i++) Seats[i] = i;
    std::cout << "Seats:";
    for (int i = 0; i < seat; i++) std::cout << std::setw(3) << Seats[i] << " ";
    std::cout << "\n";

    for (int i = 0; i < row; i++) {
        std::cout << "Row " << std::setw(2) << std::left << i << " ";
        for (int j = 0; j < seat; j++) {
            std::cout << " " << theater[i][i] << "  ";
        }
        std::cout << "\n";
    }
}
void showTheater(char剧院[][20],内排,内座){
标准::矢量座椅(座椅);
对于(int i=0;istd::是否可以将
setw
应用于
i
中的
i
您是否只需要对齐星号,或者也要对齐标题?使用printf(),有时比只处理星号要好。非常感谢。欢迎使用。如果此解决方案令您满意,您能否接受此答案?
Seats:  0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19
Row 0  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *
Row 1  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *
Row 2  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *
Row 3  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *
Row 4  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *
Row 5  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *
Row 6  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *
Row 7  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *
Row 8  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *
Row 9  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *
Row 10 *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *
Row 11 *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *
Row 12 *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *
Row 13 *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *
Row 14 *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *
Row 15 *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *
void showTheater(char theater[][20], int row, int seat) {
    std::vector<int> Seats (seat);
    for (int i = 0; i < seat; i++) Seats[i] = i;
    std::cout << "Seats:";
    for (int i = 0; i < seat; i++) std::cout << std::setw(3) << Seats[i] << " ";
    std::cout << "\n";

    for (int i = 0; i < row; i++) {
        std::cout << "Row " << std::setw(2) << std::left << i << " ";
        for (int j = 0; j < seat; j++) {
            std::cout << " " << theater[i][i] << "  ";
        }
        std::cout << "\n";
    }
}