gpt4 book ai didi

c++ - 如何使用具有负索引的(2d)数组?

转载 作者:太空宇宙 更新时间:2023-11-04 01:13:16 26 4
gpt4 key购买 nike

在 1D 中,您可以通过以下方式模拟 x 坐标:

int temp[1000];
int *x = a+500;

我们现在如何拥有网格? (类似于 a[10][-13]。)

最佳答案

您可以轻松地将 -ve 和 +ve 整数转换为 +ve 整数作为数组的索引,因为您无法使用 -ve 索引。

方法是这样的

if (index < 0)
then index = -index * 2 - 1
else index = index * 2

即-ve 索引使用奇数,+ve 使用偶数。 0 保持为 0。

关于c++ - 如何使用具有负索引的(2d)数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7200780/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com