Rethinking the structure of my program, I came to the conlusion that it is better to have the 2D grid as a private member variable of the class rather than a static one. Since the size of the grid is given before compile time and it won't change during that time, I dont need really to work with vectors anymore (and I used vectors in the first place since I am not ready to work with pointers and stuff). But when using an array I am not pleased with the way I have to initialize the array, since when the grid is large I do think the loops are a bit slow. Now I want to ask here what I should use?
test.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
// include guard
#include <vector>;
constint cols = 10;
constint rows = 10;
Class Test{
public:
Test();
~Test();
private:
std::vector< std::vector<int> > myVector;
int myArray[row][col];
};
test.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13
Test::Test(){
// initialize vector with 0s
myVector.resize(rows, std::vector<int>(cols, 0));
// initialize array with 0s
for(int i = 0; i < row; i++){
for(int j = 0; j < col; j++){
myArray[i][j] = 0;
}
}
}