【问题标题】:How can I insert a multidimensional array in another array(int not char) [closed]如何在另一个数组中插入多维数组(int not char)[关闭]
【发布时间】:2011-06-15 09:55:35
【问题描述】:

我想要一个示例来说明如何在另一个 1D int 数组中插入 2D int 数组?我在这里陷入了死胡同。

【问题讨论】:

  • 我觉得你真的想具体说明你的意思。有很多方法可以将 2D 数组的元素放入 1D 数组。如果你澄清,你实际上可能会得到一些帮助。

标签: c++ multidimensional-array int


【解决方案1】:
// 2D int array
typedef std::vector<std::vector<int> > twoDarray;

// 1D array **of 2D arrays, not ints **
std::vector<twoDarray> oneDarray;

// Instance of 2D array
oneDarray myOneDArray;

// Instance of 2D array
twoDarray myTwoDArray;

// Add this in.
myOneDArray.push_back(myTwoDArray);

【讨论】:

  • 这些不是数组。这些是向量。看看std::array
  • @Tomalak,OP 没有指定 c++0x。
  • @Nim:然后是boost::array,或者tr1::array,或者原生数组。不管你使用什么风格的 C++,你演示的都不是数组。
  • @Tomalak,推荐vector 有什么问题?
  • @Nim:没什么,但你在每一个使用向量的地方都写了“array”作为注释。
【解决方案2】:

当您说插入时,我假设您的意思是“代表”。您可以通过多种方式在 1D 数组中表示 2D 数组,只要您以某种方式在 2D 索引和 1D 索引之间进行 1 对 1 映射。例如:

storage[row*column_count + column]

将数据存储在一维数组(或vector)内的row, column 是一种典型的简单方法。

【讨论】:

    猜你喜欢
    • 2019-12-03
    • 2012-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多