comparing two objects

I have two object containing the same numbers.
the first object has numbers coming as follow: n1 = 1.2.3.4.5.6.7.8.9.10
and the second object : n2 = 2.6.3.4.8.6.7.9.5.10.1

Is there anyway to compare the two object to see if they are similar or not??

1
2
3
4
5
6
bool operator==(const Numbers& n1, const Numbers& n2){
	
	bool result;
	result = (n1==n2);
      return result;
}


I didn't find a way to reorder the second object to be able to compare theme.
Last edited on
You could sort your lists and then compare them element by element. I'd need to see more of your Numbers class to know what it is supposed to be.
hi, here is a modified and mini version of the code that represent what i want to do ;

TestList.h
1
2
3
4
5
6
7
8
9
10
11
12
#ifndef TESTLIST_H
#define TESTLIST_H
#include <iostream>
#include <list>
using namespace std;

class TestList{
	TestList();
	friend bool operator==(const TestList& l1, const TestList& l2);
};

#endif 

TestList.cpp
1
2
3
4
5
6
7
8
9
10
11
#include "TestList.h"
#include <iostream>
#include <list>

TestList::TestList(){}
bool operator==(const TestList& l1, const TestList& l2){
	
	bool result;
	result = (l1==l2); 
	return result;
}

UseTestList.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include "TestList.h"
#include <list>
#include <iostream>
using namespace std;

int main(){
	bool result;
	int num;
	list<int> list1;
	list<int> list2;

	list1.push_back(3);    list1.push_back(4);
	list1.push_back(10);  list1.push_back(15);
	list1.push_back(43);
	
	list2.push_back(10);  list2.push_back(43);
	list2.push_back(15);  list2.push_back(3);
	list2.push_back(4);
	
	cout<<"\tlist1: ";
	for(list<int>::const_iterator it1 = list1.begin(); it1 != list1.end(); it1++)
		cout<<*it1<<".";
	cout<<endl;
	
        cout<<"\tlist2: ";
	for(list<int>::const_iterator it2 = list2.begin(); it2 != list2.end(); it2++)
		cout<<*it2<<".";
	cout<<endl;

	result = operator==(list1, list2);
	cout<<"Compare Result : "<<result<<endl;
	
	return 0; 
}

The two list are the same, but the second one is not ordered
I d like to reorder the second list to make to similar to the first one.
Sometimes the answer is so simple that i really don't get how i missed it .
The only think i had to so is add:
list2.sort();
well. thank you for the help
I don't think your code is doing what you're expecting.

Line 30: You're calling operator == and passing list1 and list2. Both list1 and list2 are of type list<int>. That's going to call std::relational operator ==, not your function in testlist.cpp because the signatures are different. Your operator == (.cpp line 6) is expecting two arguments of type TestList&, not list<int>.
http://www.cplusplus.com/reference/list/list/operators/

BTW, if you were to actually call your operator ==, it would result in infinite recursion and a program crash. How do you think line 9 is going to be evaluated? That will result in a recursive call to your operator==, which results in another call to it, ad nauseum.


Last edited on
Topic archived. No new replies allowed.