Compare two different STL list
-
Hi I had two diffrent class Student { public: string name; int age; }; list _studentList1; list _studentList2; I just want to compare wheather the content of two list are same. Can any one tell me how to do that..... Below i have implemented one example for(std::list::iterator iterator1 =_studentList1.begin();studentList1.end() != iterator1; ++iterator1) { for(std::list::iterator iterator2 =_studentList2.begin();studentList2.end() != iterator2; ++iterator2) { Student* studentDetails1 = *iterator1; Student* studentDetails2 = *iterator2; if(!strcmp(studentDetails1->name ,studentDetails2->name) if(studentDetails1->age== studentDetails2->age) } } Is this is best approach to compare List ++iterator) Thanks in advance
-
Hi I had two diffrent class Student { public: string name; int age; }; list _studentList1; list _studentList2; I just want to compare wheather the content of two list are same. Can any one tell me how to do that..... Below i have implemented one example for(std::list::iterator iterator1 =_studentList1.begin();studentList1.end() != iterator1; ++iterator1) { for(std::list::iterator iterator2 =_studentList2.begin();studentList2.end() != iterator2; ++iterator2) { Student* studentDetails1 = *iterator1; Student* studentDetails2 = *iterator2; if(!strcmp(studentDetails1->name ,studentDetails2->name) if(studentDetails1->age== studentDetails2->age) } } Is this is best approach to compare List ++iterator) Thanks in advance
No, it's not the best way - the best way would be to define an operator== for Student, then use the operator== defined for std::list. Also - isn't the 'name' member a std::string? In which case that has an operator== Anyway - I'd do this:
class Student
{
public:
std::string name;
int age;
friend bool operator==(Student const& left, Student const& right)
{ return left.age==right.age && left.name==right.name; }
};
std::list<Student> _studentList1;
std::list<Student> _studentList2;Then in code, I could do
if (_studentList1 == _studentList2)
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p