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}) asked March 19th 20 at 08:38

2 answers

answered on

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.

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.

answered on

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

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++

`<`

for the class polygon. https://en.cppreference.com/w/cpp/algorithm/sortOn 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:46std::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==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