以类为元素的动态三维数组新建问题

Dreamz 2013-08-09 10:00:34
需要新建以类为元素的一个动态三维数组,这个类有大概十多个属性,在后期要对这些属性进行各种计算,而且还会对这个三维数组的大小进行扩充,对于这样一个动态数组,是用三维指针新建一个数组好一些还是用三维的vector新建一个好一些?或者有别的更好的方法?谢谢先~
...全文
106 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
Dreamz 2013-08-11
  • 打赏
  • 举报
回复
在StackOverFlow发帖提问,已经解决,采用的参考建议如下:
引用
If you know the size of all three dimensions at the time, that you write your code, and if you don't need checking for array bounds, then just use traditional arrays: const int N1 = ... const int N2 = ... const int N3 = ... A a[N1][N2][N3] If the array dimensions can onlybe determined at run time, but remain constant after program initialization, and if array usage is distributed uniformly, then boost::multi_array is your friend. However, if a lot of dynamic extension is going on at runtime, and/or if array sizes are not uniform (for example, you need A[0][0][0...99] but only A[2][3][0...3]), then the nested vector is likely the best solution. In the case of non-uniform sizes, put the dimension, whose size variies the most, as last dimension. Also, in the nested vector solution, it is generally a good idea to put small dimensions first.
赵4老师 2013-08-09
  • 打赏
  • 举报
回复
仅供参考
//在堆中开辟一个3×4×5的3维int数组
#include <stdio.h>
#include <malloc.h>
int ***p;
int i,j,k;
void main() {
    p=(int ***)malloc(3*sizeof(int **));
    if (NULL==p) return;
    for (i=0;i<3;i++) {
        p[i]=(int **)malloc(4*sizeof(int *));
        if (NULL==p[i]) return;
        for (j=0;j<4;j++) {
            p[i][j]=(int *)malloc(5*sizeof(int));
            if (NULL==p[i][j]) return;
        }
    }
    for (i=0;i<3;i++) {
        for (j=0;j<4;j++) {
            for (k=0;k<5;k++) {
                p[i][j][k]=i*20+j*5+k;
            }
        }
    }
    for (i=0;i<3;i++) {
        for (j=0;j<4;j++) {
            for (k=0;k<5;k++) {
                printf(" %2d",p[i][j][k]);
            }
            printf("\n");
        }
        printf("---------------\n");
    }
    for (i=0;i<3;i++) {
        for (j=0;j<4;j++) {
            free(p[i][j]);
        }
        free(p[i]);
    }
    free(p);
}
//  0  1  2  3  4
//  5  6  7  8  9
// 10 11 12 13 14
// 15 16 17 18 19
//---------------
// 20 21 22 23 24
// 25 26 27 28 29
// 30 31 32 33 34
// 35 36 37 38 39
//---------------
// 40 41 42 43 44
// 45 46 47 48 49
// 50 51 52 53 54
// 55 56 57 58 59
//---------------
Dreamz 2013-08-09
  • 打赏
  • 举报
回复
引用 1 楼 max_min_ 的回复:
看你实际操作这些对象的动作是怎么样的, 添加删除频繁不,量多不多,以及其他的一些操作! 来选择是动态分配还是用容器的!
这个动态三维数组的体积只会增加不会减少,不会进行实际的删除操作,而只是将其中一些元素的某个属性值标记为0来使其不参与运算,而且在实际运算时三维数组的元素总数可能达到百万级,但是目前能力有限,优化算法什么的就先不考虑了
max_min_ 2013-08-09
  • 打赏
  • 举报
回复
看你实际操作这些对象的动作是怎么样的, 添加删除频繁不,量多不多,以及其他的一些操作! 来选择是动态分配还是用容器的!
Dreamz 2013-08-09
  • 打赏
  • 举报
回复
引用 3 楼 zhao4zhong1 的回复:
仅供参考
//在堆中开辟一个3×4×5的3维int数组
#include <stdio.h>
#include <malloc.h>
int ***p;
int i,j,k;
void main() 
---------------
多谢回复,但是答非所问~

64,641

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧