【问题标题】:Displaying a Three-dimensional Array of Structs显示结构的三维数组
【发布时间】:2018-04-28 22:22:42
【问题描述】:

我正在尝试使用 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

我希望能够在特定位置输入数组,例如[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;

我正在尝试打印数组,但它没有在输出中显示:

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";
}

任何帮助将不胜感激。

【问题讨论】:

  • 您能补充更多信息吗?运行上面的代码会发生什么?你期望会发生什么?
  • 为什么需要帮助?
  • 这看起来不错,您是否遇到任何错误?
  • 已更新。谢谢你。我正在尝试打印数组,但没有显示新的输入。
  • @MikeS 在这种情况下,您需要打印array[a][b][c].Classarray[a][b][c].Teacherarray[a][b][c].noStudents。 (没有学生?那真的不能作为一个班级,是吗?`你可以在一个printf(...)中完成所有这些,你的电话。当你打印array[a][b][c]时,你会得到地址该结构 - 一个整数 - 不是其中包含的任何数据。此外,'%d' 不会打印字符串。

标签: c++ arrays multidimensional-array data-structures struct


【解决方案1】:

你不能像那样输出一个完整的结构。 printf() 不是魔法。编译器应该如何知道使用什么格式?

只需使用cout&lt;&lt; 来输出结构的每个成员:

for (int a = 0; a<3; a++)
{
    for (int b = 0; b<4; b++)
    {
        for (int c= 0; c<5; c++)
        {
            cout << array[a][b][c].Class << ' '
                 << array[a][b][c].Teacher << ' '
                 << array[a][b][c].noStudents << '\n';
        }
        cout << "\n";
    }
    cout << "\n";
}

【讨论】:

  • printf() 没有任何问题。在某些情况下,它可能比流式输出更有效。为了乐趣和利润,追踪到 stl 并查看当您将 &lt;&lt; 变为 std::cout 时发生的一切。
  • @DavidLively 在这一点上我同意你的看法。另一方面,我不明白你为什么不想使用cout&lt;&lt;。我想不出你想要向控制台输出足够多的东西以提高效率而不是写入文件的原因。根据我自己的测试,在文件上使用&lt;&lt; 非常有效(比fprintf() 快)。我还发现printf() 的语法更难使用,但这可能只是因为我不经常使用它。
【解决方案2】:

您应该只使用cout,而不是使用printf("%d\t", array[a][b][c]),这样您就必须为数组中的每个位置获取班级、教师和noStudents。所以应该是这样的

 cout << array[i][j][k].Class << ' '
      << array[i][j][k].Teacher << ' '
      << array[i][j][k].noStudents << '\n';

【讨论】:

    【解决方案3】:

    我发现这个结构没有任何价值

    struct SPS
    {
      std::string Class;
      std::string Teacher;
      int         noStudents;
    };
    
    struct SPS array[3][4][5];
    

    它是禁用的、被动的,并且没有提供有用的代码来帮助您进行其余的编程工作。

    C++ 很容易让每个实例执行独特的功能。具体来说,您应该考虑在类中实现显示(和填充)。这些函数使每个实例都能够对应用程序有用......即“show()”、“dump()”、“saveToFile()”、“restoreFromFile()”等。

    #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];
    

    注意 - 不需要单字段 get'ers 或 set'ers ...这些都是浪费程序员的时间。

    为了完成这个 MCVE,我提供:

    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);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-03-09
      • 1970-01-01
      • 2018-05-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多