Consider the following small code:
class Person {
public:
QString name;
int age;
};
int main()
{
QMultiHash<int, Person*> personHash;
Person* p1 = new Person;
p1->age = 24;
p1->name = "X";
personHash.insert(p1->age,p1);
Person* p2 = new Person;
p2->age = 24;
p2->name = "X";
if(personHash.contains(p2->age,p2)) {
cout << "Duplicate!!\n";
}
else {
cout << "Inserted!!\n";
}
return 0;
}
The output is Inserted!! and this is expected because the hash compares the pointer value and not the content.
Is there a way to check for duplicates without the need to iterate over the entries with keys of 24?
Of course it is inserted. Because you are comparing pointers (i.e. address in memory) not the person objects. And pointer p1 is of course different from p2. The simplest solution would be to store values rather than pointers in the container:
As a side benefit you do not need to take care of dynamic allocation and deallocation (which in this case could be a problem).