简体   繁体   English

创建一个动态std :: array of std :: array

[英]create a Dynamic std::array of std::array

My task requires me to create an array of arrays to store some data , where the number of row is fixed and the columns are to be decided at run-time. 我的任务要求我创建一个数组数组来存储一些数据,其中行数是固定的,列将在运行时确定。

If I was using a simple int array then this would've been a simple task but because I have to use std::array , I am lost . 如果我使用的是简单的int数组,那么这将是一个简单的任务,但是由于必须使用std :: array,我迷路了。

My attempt so far 到目前为止我的尝试

#include<iostream>
#include<array>
using std::array;
int main(){
  array<array<int,1>*,3> x;
  for(size_t i=0;i<3;i++)
      {
        x[i][0]=array<int,3>;
      }
}

which leads to the error 导致错误

array1.cpp:12:29: error: expected '(' for function-style cast or type construction x[i][0]=array; ~~~~~~~~~~~~^ 1 error generated. array1.cpp:12:29:错误:函数样式转换或类型构造x [i] [0] = array的预期'(';; ~~~~~~~~~~~~ ^^ 1错误生成。

when using clang++ 使用clang ++时

I have read that an std::array is equivalent to a normal array , I know i can use vectors but since i know how many rows i have from the beginning , I feel it to be a waste to use vectors , while I would love the added functionality of std::array when compared to a traditional array. 我已经读过std :: array等效于普通数组,我知道我可以使用向量,但是由于我从一开始就知道有多少行,所以我觉得使用向量是一种浪费,尽管我很乐意与传统数组相比,增加了std :: array的功能。 How do I implement this . 我如何实现这一点。

std::array<std::vector<int>,3> is the type you want. std::array<std::vector<int>,3>是您想要的类型。

std::vector is a dynamicly sized array. std::vector是动态大小的数组。

int main(){
  std::array<std::vector<int>,3> x;
  for(std::size_t i=0;i<3;i++)
  {
    x[i]=std::vector<int>(22);
  }
}

this creates a 3 "major" element array of 22 "minor" size. 这将创建一个3个“主要”元素数组,其大小为22个“较小”元素。

Note that column-major and row-major (which is first and which is second) is a matter of convention. 请注意,column-major和row-major(第一和第二)是惯例。 So std::vector<std::array<3,int>> is another equally valid interpretation of the requirements. 因此std::vector<std::array<3,int>>是对需求的另一个同等有效的解释。

If you are banned from using std::vector , you will have to figure out an alternative solution, possibly rolling your own. 如果您被禁止使用std::vector ,那么您将不得不找出一个替代解决方案,可能自己动手。 I'd advise against unique_ptr<int[]> (or worse, raw pointers) as they don't store the size of the element. 我建议不要使用unique_ptr<int[]> (或更糟糕的是,原始指针),因为它们不存储元素的大小。

A std::array< std::array< cannnot be dynamic in either dimension. std::array< std::array<不能在任何一个维度上都是动态的。 std::array is fixed size. std::array是固定大小的。

My task requires me to create an array of arrays to store some data , where the number of row is fixed and the columns are to be decided at run-time. 我的任务要求我创建一个数组数组来存储一些数据,其中行数是固定的,列将在运行时确定。

It sounds like your task requires using a matrix class. 听起来您的任务需要使用矩阵类。 Like boost::numeric::ublas::matrix . boost::numeric::ublas::matrix

The benefit of using a matrix class is that internally it uses one dynamic array to minimize storage overhead. 使用矩阵类的好处是,在内部它使用一个动态数组来最小化存储开销。

If you are coding this as an exercise create your own matrix class. 如果您将此编码为练习,请创建自己的矩阵类。 Otherwise use an existing one. 否则,请使用现有的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM