C Tricks(二)—— 按块访问二维数组
2016-05-12 22:17
316 查看
int mat[][5] = { { 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 } };
如我们要索引上述二维数组的某一 patch,比如第一行第二列开始的 2 行 3 列的内容,
8, 9, 10; 13, 14, 15。
int row = sizeof(mat)/sizeof(mat[0]); // 行数 int col = sizeof(mat[0])/sizeof(mat[0][0]); // 列数 int *p = (int *)mat; // 二维数组转换为一维 int org_x = 1, org_y = 2; // patch 的起始位置 int patch_height = 2, patch_width = 3; // patch 的大小 int *row_ptr = p + org_x * col + org_y; // 块的行索引 for (int i = 0; i < patch_height; ++i){ row_ptr += i * col; // 当前行,关键步,每次跳一行 for (int j = 0; j < patch_width; ++j) printf("%d ", *(row_ptr + j)); // 当前元素 putchar('\n'); }
或者这么访问元素:
for (int i = 0; i < patch_height; ++i){ for (int j = 0; j < patch_width; ++j) printf("%d ", row_ptr[i*col + j]); // 当前元素 putchar('\n'); }
相关文章推荐
- Http 请求
- 购买腾讯云服务器后的配置问题分析
- deep learning---利用caffe在vgg-face上finetuing自己的人脸数据
- 排序算法的稳定性总结
- makefile模板
- poj 1698 Just a Hook -- (线段树,区间值修改,区间求和)
- 斐波那契数 32位windows 汇编
- Servlet详细介绍
- 【spark】spark+kafka
- JS canvas 表情
- C语言学习新手常见问题
- 更改host的重定向
- CFproblemset里的241B
- 面向对象的一些心得
- 回调--闭包--同步--异步--阻塞--非阻塞
- SwipeRefreshLayout配合RecyclerView实现下拉刷新和上拉加载更多以及没有数据的显示
- mui图片预览
- [PB]-数据窗口对象(一)
- Servlet01
- SIFT特征提取分析