Message from C, C++ discussions

December 2019

— 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);
else
++it2;
}
}

— "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 https://stackoverflow.com/questions/5322104/how-portable-is-end-iterator-decrement

Message permanent page

— Unless the list is empty

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

— Or is it intended like that?

— It's my mistake

— I've changed it but it's not still working
auto end = points.end();
--end; --end;
for (auto it = points.begin(); it != end;)
{
auto it2 = ++it;
while (it2 != points.end())
{
if (it->x == it2->x && it->y == it2->y && it->z == it2->z)
it2 = points.erase(it2);
else
++it2;
}
}

Message permanent page

— I see one more problem (except that you keep saying "not working" without providing what exactly is going wrong).
Your it and it2 are equal at the start of each iteration. Because you increment it when set it2.
You should set it2 = it and then increment it2 only. And increment it at the and of the iteration (or better in the loop expression, as it was before)

Message permanent page