Re: Comparing two objects Organization: ...
Gus Gassmann wrote:
I have complicated classes that I need to test for equality. An
example would be something like
class IndexValuePair
{
int dim;
int* index;
double* value;
}
class SparseMatrix
{
int numberOfColumns;
int* start;
IndexValuePair nonzeros;
Did you mean for this to be
IndexValuePair *nonzeros;
}
I want to compare two sparse matrices.
Are there any library functions available that would let me do this?
Have you looked at std::equal to compare the arrays? You'll still have
to write some comparison operator functions to get the work done.
Ideally I would like to be able to say
if (SparseMatrix1 == SparseMatrix2)...
Not too hard to write:
bool operator==(const IndexValuePair &, const IndexValuePair &);
and the corresponding function for SparseMatrix.
I looked, but I have not been able to find anything useful.
Where did you look? What textbook are you using?
(Be gentle, I am only a beginner...)
I'm wondering if it's possible for you to rewrite your classes as
wrappers for std::map? That might make a lot of things easier to do.
Also, if you can't make assumptions about the order of the indicies in
either of the classes the easiest thing to do might be to convert the
data to a std::map and compare maps.
For example, add a function to your IndexValuePair class...
std::map<int,double> map() const {
std::map<int,double> result; // should probably be a typedef
... do the conversion work...
... and...
return result; // can be made more efficient.
}
and elsecode
bool operator==(const IndexValuePair &i1, const IndexValuePair &i2) {
return i1.map() == i2.map();
}
You may want to look for how it can be better to write an operator==
function using operator<.
You can extend this idea to SparseMatrix, by adding some function like
std::map<int,IndexValuePair> map() const; // should have a typedef
and comparison operators as needed and then use the syntax
if (SparseMatrix1 == SparseMatrix2)...
you asked for above.
LR
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]