How can I compare Dimensional arrays using iterators? My attempt is below but I gave up and used indexing instead. I plan to change comparing doubles with == to use epsilon later. Also should I be using vector of vectors?

``````//In my class, Data is defined as std::vector<std::vector<double>> Data;

bool Matrix::operator == (const Matrix& M) const
{/*
for (std::vector<std::vector<double>>::iterator Row = Data.begin(); Row != Data.end(); Row++)
{
for (std::vector<double>>iterator::Column = Row->begin(); Column != Row->end(); Column++)
{

}
}
*/
for (size_t I = 0; I < Data.size(); I++)
{
for (size_t J = 0; J < Data[I].size(); J++)
{
if (Data[I][J] != M[I][J])
return false;
}
}
return true;
}

//I used this for indexing so that I can do Matrix M;  M[I][J].
std::vector<double> Matrix::operator [](size_t I)
{
return Data[I];
}
``````

I also gave up on my multiplication using iterators to use indexing because I cannot figure it out :S

``````void Matrix::Multiply(Matrix N)
{
Matrix Product = Matrix(Data.size(), N.Data.size());
for (size_t I = 0; I < Data.size(); I++)
{
for (size_t J = 0; J < N.Data.size(); J++)
{
double SumElements = 0.0;
for (size_t K = 0; K < Data.size(); K++)
{
SumElements += Data[I][K] * N.Data[K][J];
}
Product[I][J] = SumElements;
}
}
//*this = Product;
}
``````

I just want my code as efficient as possible which is why I thought iterators would be far far better.

As far as goes, it's unnecessary to micromanage like that. However, I think that given the awkwardness of using iterators here, subscripting is cleaner anyway. Just make sure to check boundaries of both vectors.

Be a part of the DaniWeb community

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