Using vectors as member of classes

Dear all,
first of all as I'm new in the forum Hello to everybody, I'm Luca.
Hope I'll be able to help others as well as I get confidence with C++

I'm an experienced Matlab user (+10yrs), but now I have to switch to C/C++
for some of my projects and I'm trying to learn but there are some differences
in the way of coding that sometimes make me confused.

So what I'm trying to achieve is pretty straightforward:

I have a class Node defined like this:

1
2
3
4
5
6
7
8
9
class Node{
private:
 enum NodeTypes NT ; // the Nodetype is defined in the header file.
 int lab ;

public:
  Node(NodeTypes x, int l) 
      :NT(x), lab(l) {}
}

(a very simple class indeed, it just stores the type of node and a value lab associated with it. I have defined also setter and getter and other functions working on one or more Node variables.

I want to define a Grid class that contains a matrix of Nodes and other parameters (like the number of nodes on each row and the number of nodes on each column, the number of nodes of given types and so on).

In the Grid class declaration I have defined this matrix as:

NodesMat **Node ;

and I define the constructor as:

1
2
3
4
5
6
7
8
9
10
11
12
13
	Grid::Grid(int ny, int nx) 
	  :nRows(ny), nColumns(nx) {
		
		NodesMat= new Node* [ny];
		for (int i=0 ; i < (ny) ; i++){
			NodesMat[i] = new Node [nx];
			for (int j=0; j<(nx) ; j++){
				NodesMat[i][j] =Node(BasalCell,i*(nxF) + j+1) ;
			}
		}
		
	
	}


Now, I'd like to know if I there are alternative implementations
that use the vector STL class.

Or if there is any better way, all the advices are greatly appreciated (actually if you can explain me the different ways that exist and when to prefer one to the other it would be instructive as well, even though it might not be the best solution in this case).

Thanks a lot in advance for the help,
Have a nice day,
Luca
NodesMat **Node ;
Type comes first, variable name comes second.

With vector it would look like this:

1
2
3
4
5
6
7
8
9
vector<vector<Node> > mat;
Grid::Grid(int ny, int nx) : nRows(ny), nColumns(nx), mat(ny,vector<Node>(nx))
{
    for (int i=0; i<ny; i++)
    for (int j=0; j<nx; j++)
    {
        mat[i][j] =Node(BasalCell,i*(nxF) + j+1) ;	
    }
}
Hi Athar,

NodesMat **Node ;
Type comes first, variable name comes second.


you're right I made a mistake when copying the code, but I've written it correctly in my implementation.

Let me know if I've got it:
vector<vector<Node> > mat;
is used to define a matrix of Node (without specifying the size).

In the constructor:
mat(ny,vector<Node>(nx))
is used to assign to mat a ny vectors of Nodes having size nx.

I just have one doubt: which constructor for Node is used? (I guess is the one that uses no arguments, but I'd like to be sure).

An other question: I'd like to modify the Node class so that each node has an id too.
Such id would be just to know how many nodes I've created so far.

I was thinking to modify the class like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
class Node{
private:
 enum NodeTypes NT ; // the Nodetype is defined in the header file.
 int lab ;
 int id

public:
  Node(NodeTypes x, int l) 
      :NT(x), lab(l) {
         static int n=0;
         id=n++;
}
}


Would that be correct???
Thanks a lot again for the help!
I just have one doubt: which constructor for Node is used? (I guess is the one that uses no arguments, but I'd like to be sure).

Yes, it's the default constructor. When there is none, you can use push_back to add the column elements instead or pass a default Node object as the second parameter of the vector<Node> constructor.

vector<vector<Node> > mat;
is used to define a matrix of Node (without specifying the size).

Yes, it declares a vector of vectors of Node.

mat(ny,vector<Node>(nx))
is used to assign to mat a ny vectors of Nodes having size nx.

Yes. The difference is minor, but it's actually an initialization, not an assignment.

And finally, yes, this thing with the ID should be fine as it is (however, it's not thread-safe).
Last edited on
I'd like to know if I there are alternative implementations that use the vector STL class.

A typical bare-bones Matrix class uses 1D storage (unless you know your matrix sizes at compile time):

1
2
3
4
5
6
7
8
9
10
template<typename T>
class Matrix
{
    std::valarray<T> data; // or std::vector, to start with
    size_t col;
 public:
    Matrix(size_t r, size_t c) : data(r*c), col(c) {}
    T& operator()(size_t x, size_t y) { return data[x*col + y]; }
    const T& operator()(size_t x, size_t y) const { return data[x*col + y]; }
};


But if you're looking for something to use in an actual project, use an actual matrix library. Writing a competitive matrix library yourself is hard (to get a taste, google 'expression templates'). Popular libraries in C++ are boost.ublas and Eigen:
http://www.boost.org/doc/libs/1_49_0/libs/numeric/ublas/doc/index.htm
http://eigen.tuxfamily.org/
Last edited on
Topic archived. No new replies allowed.