tags:

views:

107

answers:

1

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.

+2  A: 

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())

Jérôme
That _is_ a way, for sure, but then I would use QSet<> directly. I usually prefer sequential containers (which are most of the time the most appropriate choice), so QSet<> or std::set can be considered an overkill.
Vlado Klimovský
I guess it's a matter of taste : if I need to store a list of elements without duplicates, I always choose a set. I never compared the performance though.
Jérôme
Well, I used to use the `std::set` or `std::map` a lot, until I had to fight with performance issues. Now I think twice before going that way ;-).
Vlado Klimovský