开发者

C++ multi dimensional array function parameter

How can I pass a two or multi dimensional array as a parameter of a function without defining its size??

Here is m开发者_开发知识库y example code:

 void test(int *a) { 
    a[0][0] = 100; 
 } 
 int main()  { 
    int a[2][2]; 
    test(a); 
    cout<<a[0][0]; 
 }


You can use a template for static sizes

template<int first, int second> void func(int(&array)[first][second]) {
}

Or a vector of vector for dynamic sizes

void func(std::vector<std::vector<int>> array) {
}

However, what you most definitely cannot do is use an int**. An int[] will decay to an int* but an int[][] will decay to an int*[]. Think about it- else, how would the language differentiate between an array of pointers, and a multi-dimensional array of values? You really should never use primitive arrays anyway, they're begging for trouble with no safety and implicit conversions up the wazoo. Grab a nice, safe std::array (or boost::array if you're in C++03) for static arrays, or std::vector for dynamic arrays.


If you're working exclusively with statically-sized, stack-allocated arrays, then a function template will do exactly what you're asking for:

#include <cstddef>
#include <ostream>
#include <iostream>

template<std::size_t N, std::size_t M>
void func(int (&arr)[N][M])
{
    std::cout << "int[" << N << "][" << M << "]\n";
    for (std::size_t n = 0; n != N; ++n)
        for (std::size_t m = 0; m != M; ++m)
            std::cout << arr[n][m] << ' ';
    std::cout << '\n' << std::endl;
}

int main()
{
    int i1[2][3] = { { 4, 5, 6 }, { 7, 8, 9 } };
    int i2[4][2] = { { 1, 3 }, { 5, 7 }, { 9, 11 }, { 13, 15 } };
    func(i1);
    func(i2);
}


Passing the pointer to the array. For example, if you have a bidimensional int array you'll need to pass int** p, along with the dimensions of the array.


For built-in arrays, you have to specify the size of all dimensions but the last dimension or indexing won't work.

If your goal is just to have a function that takes multi-dimensional arrays of any size, I'd consider boost::multi_array_ref (or boost::const_multi_array_ref)

Update: Since passing by pointer appears to be the answer that's getting the most attention (although I think the multi_array_ref is good... unless boost isn't available or something) then here's an example that flattens the array and doesn't limit you by array dimensions (although you still need size information to make it useful)

void f(int* array /* should probably pass in the size here - in this case, 4 */)
{
   array[3] = 9;
}

int main()
{
   int array[2][2] = { {1,2}, {3,4} };

   // Note: The array is flattened here. If you truly need to remember the multi-dimensional nature, you need to pass in enough information to specify all the dimensions... maybe something like a vector<size_t> (that's what the multi_array_ref uses). I guess if you have a limited number of dimensions then a couple size_t will work for you

   test(&array[0][0]);

   std::cout << array[1][1] << std::endl;
   return 0;
}


int a[][]

Can be passed as:

function name(int **arr) {
    //your code, you can then access it just like you would have accesses your array:
       arr[3][2]
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜