如何正确显示二维字符串数组

How to display a two-dimensional string array properly?

本文关键字:二维 字符串 数组 何正确 显示      更新时间:2023-10-16

这就是我正在制作的表格

#include <iostream>
#include <string>
using namespace std;
int main()
{
    string tables[5][14] = 
    { { "0", "3", "6", "9", "12", "15", "18", "21", "24", "27", "30", "33", "36", "2 to 1" },
    { "2", "5", "8", "11", "14", "17", "20", "23", "26", "29", "32", "35", "2 to 1" },
    { "1", "4", "7", "10", "13", "16", "19", "22", "25", "28", "31", "34", "2 to 1" },
    { "1st 12", "2nd 12", "3rd 12" },
    { "1-10", "Even", "Red", "Black", "Odd", "19-36" } };
     cout << tables << endl;
    return 0;    
}

所以我运行这段代码,我的编译器没有显示任何错误,但打印的只是一串奇怪的字母和数字,

007EF610

我以前从未遇到过这种情况,真的可以使用一些建议,感谢您的时间!

您的代码打印tables ,这是第一个字符串的地址。如果要打印字符串数组数组中包含的所有字符串,则必须自己编写一个遍历所有tables[i][j]的循环,例如:

for(int i=0; i<5; ++i)
{
  for(int j=0; j<14; ++j)
  {
    cout << tables[i][j] << endl;
  }
}

您正在尝试执行以下操作:

#include <iostream>
#include <string>
#define ROWS 5
#define COLS 14
using namespace std;
int main()
{
    string tables[ ROWS ][ COLS ] =
    {
        { "0", "3", "6", "9", "12", "15", "18", "21", "24", "27", "30", "33", "36", "2 to 1" },
        { "2", "5", "8", "11", "14", "17", "20", "23", "26", "29", "32", "35", "2 to 1" },
        { "1", "4", "7", "10", "13", "16", "19", "22", "25", "28", "31", "34", "2 to 1" },
        { "1st 12", "2nd 12", "3rd 12", "", "", "", "", "", "", "", "", "", "", "" },
        { "1-10", "Even", "Red", "Black", "Odd", "19-36", "", "", "", "", "", "", "", "" }
    };
    for ( int i = 0; i < ROWS; i++ )
    {
        for ( int j = 0; j < COLS; j++ )
        {
            cout << tables[ i ][ j ] << " ";
        }
        cout << endl;
    }
    system( "pause" );
    return 0;    
}

输出:

0 3 6 9 12 15 18 21 24 27 30 33 36 2 to 1 
2 5 8 11 14 17 20 23 26 29 32 35 2 to 1  
1 4 7 10 13 16 19 22 25 28 31 34 2 to 1  
1st 12 2nd 12 3rd 12

代码上发生的事情是:您假设cout << tables << endl能够打印整个表。但事实并非如此。 cout不能自动包含有关表的任何信息。因此,您必须正确循环它。

您获得的数字(如 007EF610)实际上是内存中tables数组的地址。

首先,正确初始化数组(您需要填写每个条目 [5][14]:

string tables[5][14] =
{
   { "0", "3", "6", "9", "12", "15", "18", "21", "24", "27", "30", "33", "36", "2 to 1" },
   { "2", "5", "8", "11", "14", "17", "20", "23", "26", "29", "32", "35", "2 to 1", "" },
   { "1", "4", "7", "10", "13", "16", "19", "22", "25", "28", "31", "34", "2 to 1", "" },
   { "1st 12", "2nd 12", "3rd 12", "", "", "", "", "", "", "", "", "", "", "" },
   { "1-10", "Even", "Red", "Black", "Odd", "19-36", "", "", "", "", "", "", "", "" }
};

然后遍历它,跳过空值:

for(int i=0;i<5;i++){
    for(int j=0;j<14;j++){
        if (!tables[i][j].empty()) {
            cout << tables[i][j] << " ";
        }
    }
    cout << endl;
}

输出:

0 3 6 9 12 15 18 21 24 27 30 33 36 2 to 1 
2 5 8 11 14 17 20 23 26 29 32 35 2 to 1 
1 4 7 10 13 16 19 22 25 28 31 34 2 to 1 
1st 12 2nd 12 3rd 12 
1-10 Even Red Black Odd 19-36 

完整代码:

#include <iostream>
#include <string>
using namespace std;
int main()
{
    string tables[5][14] =
    {
       { "0", "3", "6", "9", "12", "15", "18", "21", "24", "27", "30", "33", "36", "2 to 1" },
       { "2", "5", "8", "11", "14", "17", "20", "23", "26", "29", "32", "35", "2 to 1", "" },
       { "1", "4", "7", "10", "13", "16", "19", "22", "25", "28", "31", "34", "2 to 1", "" },
       { "1st 12", "2nd 12", "3rd 12", "", "", "", "", "", "", "", "", "", "", "" },
       { "1-10", "Even", "Red", "Black", "Odd", "19-36", "", "", "", "", "", "", "", "" }
    };
    for(int i=0;i<5;i++){
        for(int j=0;j<14;j++){
            if (!tables[i][j].empty()) {
                cout << tables[i][j] << " ";
            }
        }
        cout << endl;
    }
    return 0;
}
你知道

,在CC++中,简单地打印变量的名称不会打印它的内容,如果它不是更简单的数据类型,即intfloatlongstringchar等。

正如您所说,变量tables是一个 2D 数组。要打印其内容,您需要遍历数组中的每个元素并打印:

for(int i=0; i<5; i++) {
      for(int j=0; j<14; j++){
        cout<<tables[i][j]<<'t';
      }
      cout<<endl;
}

PS:您得到的输出 - 007EF610 - 是 2D 数组的基址,即内存中存储它的位置。

首先了解这个声明是什么意思

string tables[5][14]

这意味着表是一个字符串对象的二维数组(有 5 行和 14 列)

它将帮助您了解所犯的错误