【发布时间】:2019-07-21 19:37:29
【问题描述】:
因此,我一直在查看以下帖子,将向量转换为数组,但这种方法似乎不适用于我的用例。
How to convert vector to array
vector<array<int, 256>> table; // is my table that I want to convert
// There is then code in the middle that will fill it
int** convert = &table[0][0] // is the first method that I attempted
convert = table.data(); // is the other method to convert that doesn't work
我相信我对数据类型后端的理解是我知识不足的地方。对此的任何帮助将不胜感激
编辑:我已将 C 样式数组的形式更改为 C++ 数组
【问题讨论】:
-
vector<int[256]>看起来已经很奇怪了,应该是std::vector<std::array<int,256>> -
我不认为你可以在实践中首先使用
vector<int[256]>。你如何在其中插入元素? -
一个数组衰减为一个指针(在这种情况下为
int *)。数组数组衰减为指向数组的指针 (int *[SIZE]),而不是双指针。获取table[0][0]的地址提供了一个指向该元素的指针,即int *。用int **制作一个二维数组需要一些工作,而且通常是完成这项工作的效率最低的方法。如果我努力坚持下去,这条评论中可能包含一个很好的正式答案。 -
就 eerorika 和 πάντα ῥεῖ 而言,我在使用 C 样式数组方面没有任何问题,但我现在正在考虑将其重做为 CPP 数组,看看这是否能解决我的问题
-
@yeroc_sebrof
int**附带的本机指针算法将根据可以使用简单公式 _addr = base_addr + (row_index * row_size) + col_index寻址的连续内存块计算偏移量