Bit of a basic question. Say I've defined a multi-dimensional array as follows:
#include <vector>
using std::vector;
#define HEIGHT 5
#define WIDTH 3
// Define vector
vector<vector<int> > array2D;
// Set up size
array2D.resize(HEIGHT);
for (int i = 0; i < HEIGHT; i++)
array2D[i].resize(WIDTH);
// Put some values in
array2D[1][2] = 6;
array2D[4][1] = 5;
Now I want to re-use the array or explicitly clean up memory. Do I just call array2D.clear();
(which will hopefully automatically clean up the rows) or must I first explicitly clear each row?
// Option 1: Is this sufficient?
array2D.clear();
// Option 2: Or do I need to explicitly clear each row:
for (int i = 0; i < HEIGHT; i++)
array2D[i].clear();
array2D.clear();
Note: The vector could be made to go out of scope, but ideally I'd like to hold the vector as a class member and only resize and re-use when the dimensions change. This will be very rare.