C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Relaxing precondition on std::set_intersection algorithm

From: Peter Neiss <peter.stefan.neiss_at_[hidden]>
Date: Tue, 16 Sep 2025 20:58:26 +0200
As I understand the problem of std::set_intersection better now, i do not
believe anymore that it is a good idea to relax the precondition to make
inplace usage possible.

Instead I would like to propose and get feedback for following solution:

auto remove_missing = [](auto first1, auto last1, auto first2, auto last2,
auto comp)
{
return std::remove_if(
first1, last1,[&](auto const& elem)
{ return not std::binary_search(first2, last2, elem, comp); }
);
};

I suggest the algorithm above with the name remove_missing. It is quite
similar to std::set_intersection(_copy).
The implementation above has complexity O(N*log(M)). N = size of
first1..last1 and M = size of first2..last2

A better implementation is
auto remove_missing = [](auto first1, auto last1, auto first2, auto last2,
auto comp)
{
auto out = first1;
while (first1 != last1 && first2 != last2)
{
if (comp(*first1, *first2))
++first1;
else
{
if (!comp(*first2, *first1))
*out++ = *first1++; // *first1 and *first2 are equivalent.
else // THE else IS NEW
++first2;
}
}
return out;
};

which has Complexity O(N+M).
Are people interested in putting this algo into the standard?

My test project is here

https://godbolt.org/z/odxGG1W8q

>
>

Received on 2025-09-16 18:58:43