获取地址在数组2d中的位置
Get position of address in array 2d
要获取c++
中数组的位置,您可以执行以下操作:
class Slot
{
public:
int Color;
std::string name;
/*.... more properties*/
};
int main()
{
Slot pix[60];
Slot& randomSlot = pix[12];
randomSlot.Color = 12;
randomSlot.name = "sdsd";
Slot* addressOfSlot = &randomSlot;
Slot* arrayStart = pix;
//get the original pos
int x = addressOfSlot - arrayStart;
}
如何获得
2d array
的x和y位置呢?:Slot pix[60][21];
我想使用这种方法,因为我将使用指针/引用,我想有一个快速的方法来获得原始位置,
- 你也可以在c#中使用不安全的代码或类似的东西来做到这一点吗?
您可以通过
将大小为ROWS x COLS
的2D数组映射到大小相同的1D数组,反之亦然array2D[x][y] <-> array1D[x * COLS + y];
,其中COLS
为列数。这使用了在C或c++中数组以技术上称为行主顺序的方式存储的事实。Fortran或MATLAB使用列主顺序,因此索引是调换的。
相关文章:
- 将值指定给向量(2D)的向量中的某个位置
- 识别 2D 矢量C++中的位置
- C++ 2D 数组写入多个位置
- 创建 2D 数组0xCCCCCCCC访问冲突写入位置
- 查找 2D 向量的位置,该向量是类C++的一个元素
- 如何在C 2D向量中找到对象(而不是int)的位置
- qt如何连接QPushButton的2D数组并返回位置
- C 2D char阵列有时会在随机位置复制数据
- OPENCV(C ) - 从已知的3D对象和相机位置计算图像的2D坐标
- 如何在 2D 碰撞检测中正确解析碰撞的位置?
- (C++)在圆内夹紧 2D 位置(使用中点圆算法绘制)
- 在动态2D布尔数组中更改一个值似乎会更改不同数组中不同位置的值
- 如何将向量插入2D矢量的定义位置
- C - 输入一系列1s和0s.然后自动分配给2D数组中的相应位置
- C++ 在 2D std::vector 中查找最大值的位置
- C++2D矢量设置矢量内的位置
- 从2D纹理上的位置(x,y)计算.obj文件中的vt(u,v)坐标
- 在3D世界中的显示器上固定位置绘制2D文本-OpenGL
- 在单目视觉里程计中使用 3D-2D 运动估计的相机位置
- 如何使用 FTGL 显示文本固定位置 2d