-3
要获得c++
数组中的位置,你可以做到以下几点:获取阵列地址的位置2D
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#中做到这一点?
你会如何表示[30] [30]?你想获得2D位置的1D位置吗? – NathanOliver
你是说给定一个值,你想在数组中找到它的标记吗? –
@NathanOliver以同样的方式在其C#代表'字节[,] myArray',我敢肯定,是连续的内存在C#中,林要做到这一点在C#中,但C++是为了说明问题的唯一途径。 [内存中的二维数组](http://stackoverflow.com/questions/11575735/two-dimensional-array-in-memory) – elios264