如何正确显示二维字符串数组
How to display a two-dimensional string array properly?
这就是我正在制作的表格
#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;
}
你知道
,在C
和C++
中,简单地打印变量的名称不会打印它的内容,如果它不是更简单的数据类型,即int
,float
,long
,string
,char
等。
正如您所说,变量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 列)
它将帮助您了解所犯的错误
相关文章:
- 如何将整数字符串转换为整数的二维向量?
- 如何在Visual Studio C++中声明一个二维字符串数组?
- 有效地从文本文件中读取带有字符串索引的大型二维数组(矩阵)
- 如何将二维数组类型字符(字符串)作为函数参数传递?
- 我无法将 Excel 中的输入字符串存储为二维字符串
- 在C++中通过引用传递字符串的二维动态数组
- 如何在C++中将字符串的动态二维数组作为参数传递
- C++基于用户输入字符串长度创建二维数组
- C / C 将字符串放入二维数组中
- 将字符串放入目标C++中的二维矩阵中
- 从二维数组中的char*初始化c++字符串时,读取大小为1的无效值
- 增加二维字符串向量中外部向量的大小
- 使用 CIN 更改二维字符串数组值
- 如何正确显示二维字符串数组
- Push_back字符串在向量中的位置到二维向量
- 将字符串转换为二维数组
- C++二维数组,包含字符串和int
- C++将二维C字符串元素传递到函数中
- 将二维字符串数组与std::sort进行比较
- 输出二维字符串数组时出现segfault