Vlado Klimovský Vlado Klimovský - 2 months ago 19
C++ Question

Removing duplicates from a QList

For years I have been using the following pattern to remove duplicates from an object of the C++

std::vector
type:

std::vector<int> cont;
std::sort(cont.begin(), cont.end());
cont.erase(std::unique(cont.begin(), cont.end()), cont.end());


Now I am wondering if the same paradigm is the one to be used with the Qt
QList<>
class, or if there is a more elegant way to do it.

Answer

I don't know about performance, but what about converting the QList into a QSet ?

QList<int> myQList;
//...
QSet<int> = QSet::fromList(myQList);
// or
QSet<int> = myQList.toSet();

(and maybe convert it back to a QList if needed with QList::fromSet())

Comments