Message from C, C++ discussions

December 2019

— Glad to meet you.


Hello, how can i delete duplicate in std::list of structures?
My struct: struct Vec { double x, y, z; };
My list: std::list<Vec> points;

— Requires some big brain

— Just google “std collection remove distinct”

— I am so drunk I can’t help ya

— I wrote this code, but it doesn't work
for (auto it = points.begin(); it != --points.end(); ++it)
auto it2 = ++it;
while (it2 != points.end())
if (it->x == it2->x && it->y == it2->y && it->z == it2->z)
it2 = points.erase(it2);

Message permanent page

— "doesn't work" is not clear enough definition.

Why do you do --points.end()? What's the meaning of this?

Message permanent page

— I'm not sure that behavior of decrement operator is specified for end() of a list.

— How i think it's a previous pointer before the end

— Ok, SO telling that this is ok

Message permanent page

— Unless the list is empty

— auto it = ++it;
I think you increment it twice per iteration