【问题标题】:C++ take a user input as the number of dimensions an array has? [closed]C++ 将用户输入作为数组的维数? [关闭]
【发布时间】:2014-10-26 23:47:04
【问题描述】:

我似乎找不到任何关于这是否可能的信息。理想情况下,我会让某人输入一个像“4”这样的整数,然后它会生成一个 4 维数组。

这可能吗?

【问题讨论】:

  • 是的,有可能。你试过什么?
  • 当您说“4-d 数组”时,您的实际意思是什么?一个有 4 个元素的数组或以array[n0][n1][n2][n3] 访问的东西?虽然前者是可能的,但我认为后者在 C++ 中是不可能的。其他编程语言可能允许动态指定维度。

标签: c++ arrays dimensions declare


【解决方案1】:

正如 Stack Overflow 帖子中所解释的,“Create an N dimensional array, where N is determined at runtime (C++)”:

The dimensions of an array are part of the type, and the type must be known 
at compile time.

因此程序员必须在compile-time 之前指定数组的维度,而不是在run-time 期间。

Type checking 通常是编译器首先执行的操作之一(它在编译器控制流的semantic analysis 部分中专门找到)以确保接收到的代码没有简单的编程错误(赋值/等效错误、等)。

如果您有任何问题,请告诉我!

【讨论】:

    猜你喜欢
    • 2022-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-22
    • 1970-01-01
    相关资源
    最近更新 更多