vector中儲存指標 和 二維陣列動態分配記憶體
阿新 • • 發佈:2019-01-05
int num = 2; vector<int*> v(16); v.push_back(NULL); v.push_back(&num); v.push_back(NULL); num = 5; v.push_back(&num); v.push_back(NULL); cout << v.size() << endl; num = 6; v[0] = # cout<< *v[0] <<endl; v[0] = NULL; if(v[0]){ cout<< *v[0] <<endl; } /* 21 6 */
對比二維陣列動態記憶體分配:
AnimationSprite** m_animalMatrix;
int animalArraySize = sizeof(AnimationSprite *) * m_width * m_height;
m_animalMatrix = (AnimationSprite **)malloc(animalArraySize);
memset((void*)m_animalMatrix, 0, animalArraySize);
當用vector儲存指標的時候,就可以像陣列一樣去使用了。