I want a list that include matrixes (not implementing matrix as a list) . and I will add matrixes when I want.
Is it the best way to take a dymamic memory with matrix size(row*column) and add the pointer that reference matrix to the list. But in this way, if you want to get a location on matrix, example, for matx[3][4]
you must write *(matx+i*columnSize + j) or maybe *(matx+findlocation(i,j))
i = row, j = column

is there a way that I can just use matx[3][4] to get one value on matrix that in a list ?

Matrix is in a linked list maybe seemed unnecessary but if you want to make a game and make a agent(artificial intelligence) that play with the user. Probably to see the future actions, you will save matrixes to a list or tree...

Recommended Answers

All 8 Replies

How is your matrix defined? If it is a class than you can put it into a std::list . Also you can add a get funtion to your class that will return the element you are looking for.

class Matrix
{
    //...
    int GetData(int row, int col); // returns the value at [row][col]
    //...
};

// in main
Matrix temp(3,3); // assuming you have a constructor like this
std::list<Matrix> matrices;
matrices.push_back(temp);

How is your matrix defined? If it is a class than you can put it into a std::list . Also you can add a get funtion to your class that will return the element you are looking for.

class Matrix
{
    //...
    int GetData(int row, int col); // returns the value at [row][col]
    //...
};

// in main
Matrix temp(3,3); // assuming you have a constructor like this
std::list<Matrix> matrices;
matrices.push_back(temp);

There is no my matris define. Is there any way to get a value on a matrix in a list exactly like "matx[3][4]" with square brackets like default C matrix not a way like matx.getData(i,j) , you can do what you want: any matrix class or other thing.... I don't know maybe with one or two operator overloading.

I don't know but is it could be done just with pointers ? not with class or operator overloading...

Is there any way to get a value on a matrix in a list exactly like "matx[3][4]" with square brackets like default C matrix

The [ ] operator can be overloaded but ill admit im not very strong on operator overloading so i dont know how to do it but i know it can be done, if you are comfortable overloading operators then im sure you could find how overloading [ ] works

Sorry i cant be of more use than that but, im hopeing it helps to know it can be done

I'm confused. You want a std::list of matrices -- each node in the std::list contains a different matrix? Something on this order?

#include <list>

class matrix
{
public:
   matrix(int rows, int cols) 
   {
      data = new int*[rows];
      for(int i = 0; i < rows; i++)
         data[i] = new int[cols];
   }
private:
   int ** data;
};

int main()
{
    std::list<matrix> matrices;
}

>>There is no my matris define. Is there any way to get a value on a matrix in a list exactly like "matx[3][4]"

Using the above code -- the answer is no because the variable matrices in main() is not a 2 dimensional array or list.

If on the otherhand you don't really want that std::list then you can write an overloaded [] operator to do what you want with must the c++ class I posted.

int main()
{
   matrix matrices;
}

I'm confused. You want a std::list of matrices -- each node in the std::list contains a different matrix? Something on this order?

#include <list>

class matrix
{
public:
   matrix(int rows, int cols) 
   {
      data = new int*[rows];
      for(int i = 0; i < rows; i++)
         data[i] = new int[cols];
   }
private:
   int ** data;
};

int main()
{
    std::list<matrix> matrices;
}

>>There is no my matris define. Is there any way to get a value on a matrix in a list exactly like "matx[3][4]"

Using the above code -- the answer is no because the variable matrices in main() is not a 2 dimensional array or list.

If on the otherhand you don't really want that std::list then you can write an overloaded [] operator to do what you want with must the c++ class I posted.

int main()
{
   matrix matrices;
}
#include <iostream>

using namespace std;

int main()
{
	int ** data;

	data = new int*[5];
		for(int i = 0; i < 5; i++)
			data[i] = new int[5];
	data[3][3]=0;

	cout<<data[3][3];
	return 0;
}

This is exactly what I want, If I make a list I just add the data pointers and it will be a good matrix list :=I was thinking your way today.

data = new int*[5];

But I didn't know that.in fact I know it but I just could't think it :) Thank you for this code.

But on the other hand. If I use your matrix class How can I overload the [][] operators. one [] operator is OK(I had seen on a array class example) I can overload it but I don't know [][] operator can be overloaded If this ok ,ok

But it's not ok Probably with two operator overloading function I can handle it. one of them will be matrix with [] operator. and it will return the row start pointer and then the other will be matrix[] or matris* with [] operator and it will return the value if i thought true. I will try it tomorrow.

You need to first realize that there is no [][] operator. In fact whenever you see matrix2D[][] or matrix3D[][][] or matrixND[][]...[], you are actually seeing multiple nested calls on the [] operator.

Consider the case for a 2D matrix. If you declare the matrix like so:

int rows = 5;
int cols = 7;
int** myMatrix = new int*[rows];
for( int i=0; i<rows; i++ )
    myMatrix[i] = new int[cols];

You are actually first declaring an array of int pointers. Each one of these int pointers is then pointing at another array of ints. The way this lies in memory can be looked at like this

array[ *, *, * ]
       |  |  |
       |  |  |---> array[ i, i, i ]
       |  |
       |  |------> array[ i, i, i ]
       |
       |---------> array[ i, i, i ]

So, when you are accessing an element of a Matrix like this:

myMatrix[1][2];

You are actually first accessing the element of the pointer array at position [COLOR="Red"]1[/COLOR], and then accessing the element of the nested int array at position 2:

          1
          |
array[ *, *, * ]
       |  |  |
       |  |  |---> array[ i, i, i ]
       |  |
       |  |------> array[ i, i, i ]
       |                        |
       |                        2
       |
       |---------> array[ i, i, i ]

So, you can't really design a custom class that can in and of itself process the nested [] operators ( [][] ). You can, however, overload the [] or () operator to behave like this:

int& operator[]( int row, int col )
{
    // return by reference element at row, col
}

and then access elements like so:

myMatrix[ 3, 5 ]

Thanks very much for long reply.

I did'nt start the operator overloding chapter on my c++ book so I don't know in detail this subject.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.