Initialization of a static 2D member vector

Hi everybody.
I know the size of my vector but I am failing initializing it the correct way. It sounds like a simple task, but yeah it doesn't work.

test.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <vector>;

const int col = 10;
const int row = 10;

Class Test{
public:
	Test();
	~Test();
		
private:
        static std::vector< std::vector<int> > myVector;
};

Test::myVector.resize(col, std::vector<int>(row, 0));


I appreciate your help.
omikron.
test.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
// include guard
#include <vector>;

const int cols = 10;
const int rows = 10;

Class Test{
public:
	Test();
	~Test();
		
private:
        static std::vector< std::vector<int> > myVector;
};


test.cpp
1
2
3
#include "test.h"

std::vector< std::vector<int> > Test::myVector( rows, std::vector<int>(cols) ) ;
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>;

const int cols = 10;
const int 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;
           }
      }

}

C++11
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <vector>

struct test {

    // implicitly declared default constructor, destructor,
    // copy/move constructors and copy/move assignment operators

    private:
        static constexpr int NROWS = 10 ;
        static constexpr int NCOLS = 10 ;


        // http://www.stroustrup.com/C++11FAQ.html#member-init
        // http://www.stroustrup.com/C++11FAQ.html#uniform-init

        // in-class member initialiser: initialise NROWS x NCOLS vector with zeroes
        std::vector< std::vector<int> > myVector { NROWS, std::vector<int>(NCOLS) };

        // in-class member initialiser: initialise NROWS x NCOLS array with zeroes
        int myArray[NROWS][NCOLS] {{}} ;
};
Thank you, that is quite nice feature!
Topic archived. No new replies allowed.