We have outdated code that returns huge lists of source pointers for a heap of selected objects (we cannot use smart pointers), and we will remove duplicates from the list and also remove them from the heap.
At the moment, as recommended by the guru, I would like to try std :: list :: unique (or forward_list :: unique) instead of the std :: unique algorithm.
I read at http://en.cppreference.com/w/cpp/container/list/unique that in a “unique” predicate we should not change objects, so it’s safe to use the standard term for deleting objects that will be deleted in list :: unique?
And if so, which object in the :: unique list should be considered as a duplicate? In the implementation, gnu "b" will be deleted, but at http://www.cplusplus.com/reference/list/list/unique/ it is written that in the pred (i, i-1) element, I will be deleted, is it Is the behavior specified as standard?
Is this correct (works in gcc) code in standard or UB?
List.sort( [] (const Val *a, const Val *b) {
return *a < *b;
});
List.unique([] (const Val *a, const Val *b) {
if (*a == *b) {
delete b; // (1) working in gcc 4.6
// or (2) delete a (elsewhere)?
return true;
}
return false;
}) ;
Update # 1
Mike's explanation was most useful, but for now, we use this solution:
struct RawPtrEq {
bool operator()(const Val a, const Val b) { return *a == *b;}
};
auto it = adjacent_find( begin(List), end(List), RawPtrEq() );
while(it != end(li)) {
delete *it;
it = List.erase(it);
it = adjacent_find( it, end(List), RawPtrEq() );
}