3572
- 收藏
- 点赞
- 分享
- 举报
动态二维三维数组
#include <stdio.h>
#include <malloc.h>
// pre-declaration
template < class T, bool bInternal > class Array3;
// template class Array2
template <class T, bool bInternal=false>
class Array2 {
public:
typedef Array2<T, bInternal> _Myt;
typedef T value_type;
typedef T* pointer;
typedef const T* const_pointer;
typedef T& reference;
typedef const T& const_reference;
Array2(int x, int y) : a(x), b(y) {
data = (pointer)malloc(sizeof(value_type)*x*y);
}
~Array2() {
if(!bInternal)
free(data);
}
pointer operator [] (int i) {
ASSERT(i>= 0 && i < a);
return data + i * b;
}
const_pointer operator [] (int i) const {
ASSERT(i>= 0 && i < a);
return data + i * b;
}
protected:
// used in template class Array3
Array2(value_type* ptr, int x, int y) : data(ptr), a(x), b(y) {
};
pointer data;
int a, b;
friend Array3<T, true>;
friend Array3<T, false>;
};
// template class Array3
template <class T, bool bInternal=false>
class Array3 {
public:
typedef Array3<T, bInternal> _Myt;
typedef Array2<T, true> a2type;
typedef const Array2<T, true> const_a2type;
typedef T value_type;
typedef T* pointer;
typedef const T* const_pointer;
typedef T& reference;
typedef const T& const_reference;
Array3(int x, int y, int z) : a(x), b(y), c(z) {
data = (pointer)malloc(sizeof(value_type)*x*y*z);
}
~Array3() {
if(!bInternal)
free(data);
}
a2type operator [] (int i) {
ASSERT(i>= 0 && i < a);
return a2type(data + i * b * c, b, c);
}
const_a2type operator [] (int i) const {
ASSERT(i>= 0 && i < a);
return a2type(data + i * b * c, b, c);
}
protected:
// used in template class Array4
Array3(value_type* ptr, int x, int y, int z) : data(ptr), a(x),
b(y), c(z) {
};
pointer data;
int a, b, c;
};
int main(int argc, char* argv[])
{
Array3<int> a3(4, 3, 2);
for(int i=0;i<4;i++)
{
for(int j=0;j<3;j++)
{
for(int k=0;k<2;k++)
{
printf("&a[%d][%d][%d] = %p\n", i, j, k, &a3[i][j][k]);
}
}
}
return 0;
}
还不是很完善
照样子也容易写出动态的四维数组实现,不过可能实际意义不大
如果需要,自己照葫芦画瓢吧,反正不难
我来回答
回答0个
时间排序
认可量排序
暂无数据
或将文件直接拖到这里
悬赏:
E币
网盘
* 网盘链接:
* 提取码:
悬赏:
E币
Markdown 语法
- 加粗**内容**
- 斜体*内容*
- 删除线~~内容~~
- 引用> 引用内容
- 代码`代码`
- 代码块```编程语言↵代码```
- 链接[链接标题](url)
- 无序列表- 内容
- 有序列表1. 内容
- 缩进内容
- 图片
相关问答
-
2019-01-25 14:18:38
-
2018-12-26 10:56:44
-
2012-12-04 12:57:16
-
2018-10-15 15:41:19
-
2017-11-22 13:27:05
-
2020-10-30 14:02:03
-
2015-11-03 19:03:43
-
2019-01-07 13:48:46
-
2020-09-02 15:44:52
-
2020-08-27 10:15:16
-
2020-11-18 10:37:07
-
2021-02-09 15:40:42
-
2018-09-03 17:52:30
-
2019-01-28 15:53:00
-
2018-12-12 11:30:02
-
2018-12-30 12:31:13
-
2018-12-26 10:49:07
-
2020-11-12 16:44:37
-
2019-01-17 14:12:45
无更多相似问答 去提问
点击登录
-- 积分
-- E币
提问
—
收益
—
被采纳
—
我要提问
切换马甲
上一页
下一页
悬赏问答
-
5hisi3516cv610 + gc4336p 夜晚很模糊
-
5AIISP(功能演示,SC4336P为BGGR,强制转RGGB,会导致颜色异常)
-
5rv1106使用luckfox的SDK,设备树和驱动都写好了,结果设备文件没有生成
-
5海思3516cv610中如何进行SD卡升级,根据官方文档操作,烧录进板子时,走的默认uboot,没有执行uboot升级。
-
5G610Q-IPC-38E 夜晚很暗 有什么办法解决吗 已经补光了
-
10转换模型时,SoC版本里没显示hi3516cv610芯片
-
5hisi3516cv610 使用 yolov8n 模型训练 要如何提高 这里识别的是人
-
10有人在海思平台接过SC035HGS吗
-
5关于hi3519dv500,以SD卡虚拟 U 盘操作
-
5ss928 sample_venc代码移植到openEuler24.03上执行报错 [sample_comm_vi_start_dev]-1068: vi set dev attr failed wi
举报反馈
举报类型
- 内容涉黄/赌/毒
- 内容侵权/抄袭
- 政治相关
- 涉嫌广告
- 侮辱谩骂
- 其他
详细说明
提醒
你的问题还没有最佳答案,是否结题,结题后将扣除20%的悬赏金
取消
确认
提醒
你的问题还没有最佳答案,是否结题,结题后将根据回答情况扣除相应悬赏金(1回答=1E币)
取消
确认

微信扫码分享
QQ好友