C++ 显示三维结构数组
我正在尝试使用3D结构数组创建学校课程计划C++ 显示三维结构数组,c++,arrays,multidimensional-array,data-structures,struct,C++,Arrays,Multidimensional Array,Data Structures,Struct,我正在尝试使用3D结构数组创建学校课程计划 struct SPS { string Class; string Teacher; int noStudents; }; struct SPS array[3][4][5]; 概念视图: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 我希望能够在特定
struct SPS
{
string Class;
string Teacher;
int noStudents;
};
struct SPS array[3][4][5];
概念视图:
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
我希望能够在特定位置输入到数组中,例如[1][2][2]
这是我目前拥有的代码,但它按预期执行
cout << "Class:" << endl;
cin >> array[1][2][2].Class;
cout << "Teacher:" << endl;
cin >> array[1][2][2].Teacher;
cout << "Number of students:" << endl;
cin >> array[1][2][2].noStudents;
cout数组[1][2][2]。类;
cout数组[1][2][2]。教师;
cout数组[1][2][2];
我正在尝试打印阵列,但它在输出中没有显示:
for (int a = 0; a<3; a++)
{
for (int b = 0; b<4; b++)
{
for (int c= 0; c<5; c++)
{
printf("%d\t", array[a][b][c]);
}
cout << "\n";
}
cout << "\n";
}
for(int a=0;a您不能像那样输出整个结构。printf()
不是魔术。编译器应该如何知道使用什么格式
只需使用cout
和而不是使用printf(“%d\t”,数组[a][b][c])
您应该只使用cout
,这样您就必须获得数组中每个位置的班级、老师和学生的信息。因此,它应该是
cout << array[i][j][k].Class << ' '
<< array[i][j][k].Teacher << ' '
<< array[i][j][k].noStudents << '\n';
cout我发现这个结构没有任何值
struct SPS
{
std::string Class;
std::string Teacher;
int noStudents;
};
struct SPS array[3][4][5];
它是禁用的、被动的,并且没有提供任何有用的代码来帮助您完成其余的编程工作
C++很容易容纳每个实例执行独特的功能。具体来说,您应该考虑在类中实现显示(和填充)。这些函数使每个实例都能对应用程序有效地执行……即“显示())、“DUMPE()”、“SaveToFielEf())、“RealEfFielf())等。
#包括
#包括
#包括
结构SPS
{
私有封装
std::字符串类;
字符串教师;
内特学生;
公众:
//类实例“show”本身
std::string show()
{
std::stringstream-ss;
ss您可以添加更多信息吗?当您运行上述代码时会发生什么?您期望发生什么?为什么需要帮助?看起来很好。您是否收到任何错误?更新。谢谢。我正在尝试打印数组,但新的输入没有显示。@MikeS在这种情况下,您需要打印array[a][b][c].Class
,数组[a][b][c]。教师
,和数组[a][b][c]。学生
(没有学生?那真的不符合班级资格,是吗?`你可以在一个printf(…)
中完成所有这些,或者不完成,你的呼叫。当你只打印数组[a][b][c]
,您将获得该结构的地址-一个整数-而不是其中包含的任何数据。此外,'%d'不会打印字符串。printf()没有问题
。在某些情况下,它可能比流式输出效率更高。为了乐趣和利润,追踪stl并查看您@时发生的所有事情。在这一点上,我确实同意您的看法。另一方面,我不明白您为什么不想使用cout
和
struct SPS
{
std::string Class;
std::string Teacher;
int noStudents;
};
struct SPS array[3][4][5];
#include <iostream>
#include <sstream>
#include <string>
struct SPS
{
private: // for encapsulation
std::string Class;
std::string Teacher;
int noStudents;
public:
// class instance 'show's itself
std::string show()
{
std::stringstream ss;
ss << Class << ' ' << Teacher << " " << noStudents;
return ss.str();
}
// for this MCVE, a simple replacement for file i/o
void fill(int a, int b, int c)
{
std::string s =
std::to_string(a)
+ '.' + std::to_string(b)
+ '.' + std::to_string(c);
Class = "\n Class " + s;
Teacher = " Teacher " + s;
noStudents = c;
}
// add saveToFile(), restoreFromFile(), dump()
};
struct SPS array[3][4][5];
class T601_t // a simple test
{
public:
T601_t() = default;
~T601_t() = default;
int exec(int , char** )
{
fill3dArray();
show3dArray();
return 0;
}
private: // methods
// specific to this MCVE, a quick and dirty fill
void fill3dArray()
{
for (int a = 0; a<3; a++)
{
for (int b = 0; b<4; b++)
{
for (int c = 0; c < 5; c++)
{
array[a][b][c].fill(a, b, c);
// record -----^^^^ self filling
}
}
}
}
// your code would enter the a, b, and c from user prompts?
// or perhaps a file read?
// specific to this MCVE, invoke the show method of
// each element of the array
void show3dArray()
{
for (int a = 0; a<3; a++)
{
for (int b = 0; b<4; b++)
{
for (int c= 0; c<5; c++)
{
std::cout << array[a][b][c].show() << '\n';
// record ------------------^^^^ self showing
// it is trivial for this cout to prefix the show
// with a triple for readability or diagnostics
}
std::cout << "\n";
}
std::cout << "\n";
}
}
}; // class T601_t
int main(int argc, char* argv[])
{
T601_t t601;
return t601.exec(argc, argv);
}