将数字和字符初始化为二维数组

Initializing a number and a char to a 2D array

我必须实现一副带有数字和字母的纸牌。这是我到目前为止所做的:

string deck [6][6] =
{
    {1A, 1B, 1C, 1D},
    {2A, 2B, 2C, 2D},
    {3A, 3B, 3C, 3D},
    {4A, 4B, 4C, 4D},
    {  ,   ,   ,   };

};
int main ()
{
   cout << deck[0][0] << endl;
}

我收到一个错误:

invalid suffix 'A' on integer constant

您收到此错误是因为您需要将字符串用双引号引起来。如果你想使用静态 initialization/declaration,它应该看起来像这样:

std::string deck[4][4] = {
    { "1A", "1B", "1C", "1D"}, 
    { "2A", "2B", "2C", "2D"}, 
    { "3A", "3B", "3C", "3D"}, 
    { "4A", "4B", "4C", "4D"}
};

您可以使用两个嵌套的 for 循环来显示牌组的全部内容:

for (int r=0; r < 4; ++r) {
    for (int c=0; c < 4; ++c) {
        if (c > 0) {
            cout << " ";
        }
        cout << deck[r][c];
    }
    cout << "\n";
}