最近剛學C++ 常常會用到陣列記憶體配置 先新增Row 再new Column就對了 其實還蠻方便、直觀的
#include <iostream> int main() { int height, width; int **arr; std::cin >> height >> width; arr = new int*[height]; for (int i = 0 ; i < height ; i++) arr[i] = new int [width]; for (int i = 0 ; i < height ; i++) { for (int j = 0 ; j < width ; j++) { arr[i][j] = i + 2; std::cout << arr[i][j] << " "; } std::cout << std::endl; } return 0; }
刪除也是一樣的概念囉~
for (int i = 0 ; i < height ; i++) delete [] arr[i]; delete [] arr;
全站熱搜
留言列表