0

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 .

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.

when using 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. How do I implement this .

6
  • You are required to use std::array to implement this? Commented Dec 10, 2018 at 16:20
  • 1
    You have an array of array pointers, so you would need to assign an array pointer, not an array. You could have an array of arrays. Commented Dec 10, 2018 at 16:22
  • You can't do this with just std::array, it is always set at compile time. Commented Dec 10, 2018 at 16:22
  • There is no such thing as "dynamic std::array". You can use std::vector instead. Commented Dec 10, 2018 at 16:23
  • 1
    If std::array is equivalent to a normal (compile time) array (int a[5];) then std::vector is equivalent to a normal dynamic array (int* a = new int[5];). Commented Dec 10, 2018 at 16:24

2 Answers 2

5

std::array<std::vector<int>,3> is the type you want.

std::vector is a dynamicly sized array.

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.

Note that column-major and row-major (which is first and which is second) is a matter of convention. So std::vector<std::array<3,int>> is another equally valid interpretation of the requirements.

If you are banned from using std::vector, you will have to figure out an alternative solution, possibly rolling your own. I'd advise against unique_ptr<int[]> (or worse, raw pointers) as they don't store the size of the element.

A std::array< std::array< cannnot be dynamic in either dimension. std::array is fixed size.

Sign up to request clarification or add additional context in comments.

1 Comment

std::vector uses heap allocation and random time
0

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.

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.