如何获得3D数组的尺寸?

时间:2015-01-16 12:16:37

标签: c++ memory allocation multidimensional-array

我很高兴在C ++ 11中发现我们可以使用“auto”分配多个维度,但是我得到了奇怪的维度值,定义为“auto a = new float [10] [20] [30]”和“漂浮a1 [10] [20] [30]”。请帮助查看以及任何可能的解决方案。

============= a.cpp =========================

#include <stdio.h>
#include <string.h>
#include <iostream>

template<class T>
size_t size(T const & a ) {  return sizeof a / sizeof (a[0]);}

int main(){
auto  a=new float[10][20][30];
float a1[10][20][30];
std::cout<<size(a)<<" "<<size(a[0])<<"  "<<size(a[0][0])<<"\n";
std::cout<<size(a1)<<" "<<size(a1[0])<<"  "<<size(a1[0][0])<<"\n";
delete[] a;
}

==========结果========================

  

XUs-Mac-mini:桌面XU $ g ++ -std = c ++ 11 a.cpp -o a

     

XUs-Mac-mini:桌面XU $ a

     

0 20 30

     

10 20 30

2 个答案:

答案 0 :(得分:2)

您可以使用模板执行以下操作:

#include <iostream>

template <typename T, size_t c0, size_t c1, size_t c2>
static size_t countof_0(const T (&arr)[c0][c1][c2]) {
    return c0;
}

template <typename T, size_t c0, size_t c1, size_t c2>
static size_t countof_1(const T (&arr)[c0][c1][c2]) {
    return c1;
}

template <typename T, size_t c0, size_t c1, size_t c2>
static size_t countof_2(const T (&arr)[c0][c1][c2]) {
    return c2;
}

int main() {
    double my_3d_array[21][11][32];

    std::cout << countof_0(my_3d_array) << "\n";
    std::cout << countof_1(my_3d_array) << "\n";
    std::cout << countof_2(my_3d_array) << "\n";
}

如果你可以使函数constexpr,那么你应该。

答案 1 :(得分:0)

这是基于Chiris&#39;的代码。上面的想法,5个维度,但是很喜欢:

        #include <stdio.h>
        #include <string.h>


        //-----------------------------------------------------------------
        //Get Dimension from 5D array
        //-----------------------------------------------------------------
        template <typename T, size_t c0, size_t c1, size_t c2,size_t c3,size_t c4>
        static size_t size1(const T (&arr)[c0][c1][c2][c3][c4]) {
            return c0;
        }
        template <typename T, size_t c0, size_t c1, size_t c2,size_t c3,size_t c4>
        static size_t size2(const T (&arr)[c0][c1][c2][c3][c4]) {
            return c1;
        }
        template <typename T, size_t c0, size_t c1, size_t c2,size_t c3,size_t c4>
        static size_t size3(const T (&arr)[c0][c1][c2][c3][c4]) {
            return c2;
        }
        template <typename T, size_t c0, size_t c1, size_t c2,size_t c3,size_t c4>
        static size_t size4(const T (&arr)[c0][c1][c2][c3][c4]) {
            return c3;
        }
        template <typename T, size_t c0, size_t c1, size_t c2,size_t c3,size_t c4>
        static size_t size5(const T (&arr)[c0][c1][c2][c3][c4]) {
            return c4;
        }
        //-----------------------------------------------------------------
        //Get Dimension from 4D array
        //-----------------------------------------------------------------
        template <typename T, size_t c0, size_t c1, size_t c2,size_t c3>
        static size_t size1(const T (&arr)[c0][c1][c2][c3]) {
            return c0;
        }
        template <typename T, size_t c0, size_t c1, size_t c2,size_t c3>
        static size_t size2(const T (&arr)[c0][c1][c2][c3]) {
            return c1;
        }
        template <typename T, size_t c0, size_t c1, size_t c2,size_t c3>
        static size_t size3(const T (&arr)[c0][c1][c2][c3]) {
            return c2;
        }
        template <typename T, size_t c0, size_t c1, size_t c2,size_t c3>
        static size_t size4(const T (&arr)[c0][c1][c2][c3]) {
            return c3;
        }

        //-----------------------------------------------------------------
        //Get Dimension from 3D array
        //-----------------------------------------------------------------
        template <typename T, size_t c0, size_t c1, size_t c2>
        static size_t size1(const T (&arr)[c0][c1][c2]) {
            return c0;
        }
        template <typename T, size_t c0, size_t c1, size_t c2>
        static size_t size2(const T (&arr)[c0][c1][c2]) {
            return c1;
        }
        template <typename T, size_t c0, size_t c1, size_t c2>
        static size_t size3(const T (&arr)[c0][c1][c2]) {
            return c2;
        }

        //-----------------------------------------------------------------
        //Get Dimension from 2D array
        //-----------------------------------------------------------------
        template <typename T, size_t c0, size_t c1>
        static size_t size1(const T (&arr)[c0][c1]) {
            return c0;
        }
        template <typename T, size_t c0, size_t c1>
        static size_t size2(const T (&arr)[c0][c1] ) {
            return c1;
        }

        //-----------------------------------------------------------------
        //Get Dimension from 1D array
        //-----------------------------------------------------------------
        template <typename T, size_t c0>
        static size_t size1(const T (&arr)[c0]) {
            return c0;
        }


        /*
        int main() {
            float my_3d_array[11][22][33][44] ;

            cout << size1(my_3d_array) << "\n";
            cout << size2(my_3d_array) << "\n";
            cout << size3(my_3d_array) << "\n";
            cout << size4(my_3d_array) << "\n";    
        }*/