-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