本帖最后由 impossible 于 2016-8-21 10:14 编辑
帮你改了一下代码
- #include <iostream>
- #include <cstring>
- template<typename T>
- void* list_slices(void* listA,int y,int z,int init_x, int end_x, int init_y, int end_y)
- {
- unsigned size=(end_x-init_x+1)*(end_y-init_y+1)*z;
- void *listB=reinterpret_cast<void*>(new T[size]);
-
- memcpy(listB,reinterpret_cast<T(*)[y][z]>(listA)+init_x,size*sizeof(T));
-
- return listB;
- }
- int main()
- {
- int a[3][3][3]={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};
-
- int (*b)[3][3]=reinterpret_cast<int(*)[3][3]>(list_slices<int>( a,3,3, 1,2, 0,2));
-
- for(int x=0;x<2;x++)
- for(int y=0;y<3;y++)
- for(int z=0;z<3;z++)
- std::cout<<b[x][y][z]<<std::endl;
- delete []b;
-
- return 0;
- }
复制代码
y,z分别对应原数组那后两个框,返回值要用强制转换为数组指针,T为数组类型,检查要取出的小部分有没有超出被取出数组的长度就不写了. |