How to remove identical items from list?

Suppose given a class T and a list of objects of class list<T>. How to remove from him the same items? (For example, the list {a, b, a, c, b} to transform in {a, b, c})
March 19th 20 at 08:38
March 19th 20 at 08:40
Solution
In the General case sorted, go through the list and removes adjacent equal elements.

But maybe it is necessary to solve the problem more radically - instead of list to use a more suitable container - set? In set items are already sorted and unique.
In the program, you will use this list. About the sorting is not completely understood. We're not numbers, and abstract objects. For example, polygons. How to sort them and why? - zoey_Zeml commented on March 19th 20 at 08:43
You also have the criterion of "sameness" of the landfill site, here on it and sort. You only need to override operator < for the class polygon. https://en.cppreference.com/w/cpp/algorithm/sort
On account of the set - if you just paste in order to set the elements of list in the result set you will get what you need. For comparison we use the same operator <, as in sort. - deshaun_Bergstrom88 commented on March 19th 20 at 08:46
@zoey_Zeml, https://ru.cppreference.com/w/cpp/algorithm/unique

std::list has its own
https://ru.cppreference.com/w/cpp/container/list/sort
https://en.cppreference.com/w/cpp/container/list/unique
left to write your "comparator". or operator==

How to remove from him the same items?

We're not numbers, and abstract objects. For example, polygons. How to sort them and why?

And how are you going to determine uniqueness? !=?

and Yes https://en.cppreference.com/w/cpp/algorithm/stable_sort - rahsaan83 commented on March 19th 20 at 08:49
I think that in some cases the option of unique will run faster, and in some cases slower than the sort. We must look at the sort class and the size of the list. - deshaun_Bergstrom88 commented on March 19th 20 at 08:52
March 19th 20 at 08:42
Solution
Create a collection ensuring uniqueness and add all the values from the list. Get a collection of unique elements. If you output the desired list, cleaning up old or create a new one, then it adds all the unique items from the collection you created.

By the time the work will be O(n * (n - a)), where N is the number of elements in the list, and the number of duplicates. Well, if we very roughly, O(n^2).

Find more questions by tags C++