-1

I have defined an array of vectors, and want to pass this array to a function.

Here's my code:

void gridlist(std::vector<int> *grid, int rows, int cols){
    ..... }

int rows=4;
int cols=5;
std::vector<int> grid[rows][cols];

gridlist(grid,rows,cols);

The only method which has worked for me to pass arrays to a function was by pointer (*) ?.

I've tried different method according to following descriptions: Passing a 2D array to a C++ function, but I didn't manage to get it working.

I always get the following error:

could not convert '(std::vector<int> (*)[cols])(& grid)' from 'std::vector<int> (*)[cols]' to 'std::vector<int>'
       gridlist(grid,rows, cols);

I don't know what I am doing wrong. What is, in general, considered the best/safest method to do that task?

Community
  • 1
  • 1
Alex Gors
  • 113
  • 1
  • 6
  • 3
    Using vector of vectors or `std::array` of vectors seems good. – MikeCAT Jun 16 '16 at 15:45
  • 1
    Have you tried, as a good two-star programmer would do, using std::vector **grid , or do you want the array sizes passed as type parameters? – lorro Jun 16 '16 at 15:48
  • Why don't you just pass a vector of vectors? – Jesper Juhl Jun 16 '16 at 15:51
  • 1
    @lorro, That won't work; a 2D arrays decays into a pointer to a 1D array, not a pointer to a pointer. – chris Jun 16 '16 at 15:54
  • 1
    @lorro, please explain yourself. Who is good 2-star programmer? – SergeyA Jun 16 '16 at 15:54
  • "I have defined an array of vectors" - no, you defined an array of arrays of `std::vector`, and non-standard at that (constant `rows` and `cols` can address the latter). – WhozCraig Jun 16 '16 at 15:57
  • Thank you everybody for all those different solutions, I decided to go with a 3D vector, as i still couldnt pass the array to the function without getting any errors. The code is working now. Thanks! – Alex Gors Jun 16 '16 at 19:05

3 Answers3

0

You may use a template:

template<int rows, int cols>
void gridlist(std::vector<int> (&grid)[rows][cols]){

}

int main()
{
const int rows=4;
const int cols=5;
std::vector<int> grid[rows][cols];

gridlist(grid);
}

there is additional change in your code, int rows=4; to const int rows=4; this is because VLA (variable length arrays) are non standard in C++. If you want dynamic array then consider using vector of vectors of vectors (whhooh) instead: std::vector<std::vector<std::vector<int>>>

marcinj
  • 44,446
  • 9
  • 70
  • 91
0
void gridlist(std::vector<int> *grid, int rows, int cols){
    ..... }

int rows=4;
int cols=5;
std::vector<int> grid[rows][cols];

gridlist(grid,rows,cols);

Here grind has the type std::vector<int>[][], while grindlist() expects std::vector<int> *. You'd have to either have to make grindlist() expect std::vector<int>[] *, or make sure grind is a std::vector<int>[]

I'd propose to prefer std::array<> instead:

template<int rows, int cols>
void gridlist(std::array<std::array<std::vector<int>, cols>, rows>& grid) {
    ..... }

constexpr int rows = 4;
constexpr int cols = 5;
std::array<std::array<std::vector<int>, cols>, rows> grid;

gridlist(grid);
cdonat
  • 2,670
  • 13
  • 24
0

You can't pass the 2D array directly if you want variable size of both dimensions.

One solution is to fix the size of cols and declare your function like:

void gridlist1(std::vector<int> grid[][5],int rows){...}

If it isn't acceptable to fix the size of one dimension, you can use a temp variable instead:

void gridlist(std::vector<int> **grid,int rows, int cols){...}

and call it like:

const int rows=4;
const int cols=5;

std::vector<int> grid[rows][cols]; 

// Temp variable
std::vector<int>* temp[rows];
for (int i = 0; i < rows; ++i) temp[i] = grid[i];

gridlist(temp,rows,cols);
         ^^^^

However, I would use vector of vector of vector instead of a traditional c-style array.

4386427
  • 33,845
  • 4
  • 32
  • 53
  • Thats what i actually did to continue working, but i needed the variable size, thanks for the workaround with the temp variable! – Alex Gors Jun 16 '16 at 19:07