如何打印 4 行 13 列的对象向量?

问题描述 投票:0回答:1

我整个早上都在尝试打印出我的 m_deck 中的卡片,共 13 行 4 列,但我一辈子都无法弄清楚。目前它仅在一栏中打印。我正在使用 CLion。我真的需要一些帮助来解决这个问题。谢谢!

Deck.cpp

void Deck::show() {
    for (int i = 0; i < 4; i++) {
        for (int j = 0; j < 13; j++) {
            m_deck[i * 13 + j].show();
            cout << "  ";
        }
        cout << "\n";
    }
};

卡片.cpp

void Card::show() {
    ...
    cout << m_rank << " of " << m_suite << ": " << rank << suite << "\n";
};
c++ vector clion
1个回答
0
投票

尝试:

void Deck::show() {
    static constexpr unsigned rowCount = 13;
    static constexpr unsigned columnCount = 4;

    // Iterate the 13 rows
    for (auto i = 0; i < rowCount; i++) {

        // Iterate trough the columns
        for (auto j = 0; j < columnCount; j++) {
            m_deck[i * columnCount + j].show();
            cout << "  ";
        }
        cout << "\n";
    }
};
© www.soinside.com 2019 - 2024. All rights reserved.