I have a QList<MyData>, where MyData have 2 member, int id (unique) and QString name. I want to remove all duplicate entries based on name, and that entry must the highest id between other object that having the same name. Any suggestion of how to doing it in the fastest way? Performance is a very important factor here.
Some of my idea after Google-ed for the whole day:
qStableSort()it based on id (descending), then loop through theQList, then for each entry, copy the entry to another newQListwhen thenameis not exist on the newQList- use
QList::toSet(which remove all duplicate entry), and provide operator==() and a qHash() implementation based onname, but the unique entry may not have the highest id - use
std::list::unique, but I’m not sure how it works.
std::list::uniquecan take as argument a function with the following properties:So in your case you could use the folllowing function:
To call it simply do,
Notice that you need to sort first the
std::listEdit
It seems that you can use
std::uniquewithQt containers(ifQtis built with STL support). So in this case you could do the following: