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: Sun, 14 Sep 2025 17:50:02 +0200
To relax the preconditions means to constrain the algorithm implementation
more.
Here is a example to illustrate what I want to achieve. I will have to
write my own implementation of set_intersection for work, but I think the
stl should provide the algorithms as good as possible.

#include <algorithm>
#include <vector>
#include <iostream>
#include <iterator>

int main()
{
std::vector A{4,1,6,7};
std::vector B{1,3,4};

std::sort(A.begin(), A.end());
std::sort(B.begin(), B.end());

// needed for current cpp standard
std::vector<int> C;
std::set_intersection(A.begin(), A.end(), B.begin(), B.end(),
std::back_inserter(C));
std::copy(C.begin(), C.end(), std::ostream_iterator<int>(std::cout, " "));

std::cout << std::endl;

// illegal, input and output overlap, but works, no memory allocation for
output
auto end = std::set_intersection(A.begin(), A.end(), B.begin(), B.end(),
A.begin());
std::copy(A.begin(), end, std::ostream_iterator<int>(std::cout, " "));

std::cout << std::endl;
// restore A
A = std::vector{4,1,6,7};
std::sort(A.begin(), A.end());

// also illegal, input and output overlap
end = std::set_intersection(A.begin(), A.end(), B.begin(), B.end(),
B.begin());
std::copy(B.begin(), end, std::ostream_iterator<int>(std::cout, " "));
return 0;
}

Am So., 14. Sept. 2025 um 16:46 Uhr schrieb Andre Kostur <andre_at_[hidden]>:

> Perhaps an example of this working? What are you passing in as the
> OutputIterator? If the algorithm isn’t writing to the OutputIterator while
> it’s finding elements, then when it finds a missing element, it would have
> to go back and copy the first how many ever elements that did match. But,
> the those are InputIterators: you may not be able to rewind those iterators
> back to the beginning. (Iterators on cin, for example)
>
> It also means that wherever the OutputIterator points at may never have
> been written to (consider output iterators on cout) in the case that the
> intersection is all elements of both sequences. The caller would have to
> compare the result with first1 and first2 to figure out if they got back an
> iterator to either of the inputs.
>
> On Sun, Sep 14, 2025 at 6:52 AM Peter Neiss via Std-Proposals <
> std-proposals_at_[hidden]> wrote:
>
>> The std::set_intersection algorithm forbids overlap of the input ranges
>> with the output range, but if first1 or first2 and result would be equal,
>> the algorithm could still work(see cppreference for possible
>> implementation).
>> This would allow inplace execution without memory allocation, which is a
>> big plus.
>>
>> 26.8.7.4 set_intersection (wording)
>>
>> replace: The resulting range does not overlap with either of the original
>> ranges. <https://eel.is/c++draft/set.intersection#2.sentence-2>
>> with: result may be equal to first1 or first2, otherwise the resulting
>> range does not overlap with either of the original ranges.
>>
>>
>> PS: I am quite sure my statement is correct and I am not making a fool
>> out of myself. Still confirmation of my claim would be great.
>>
>> --
>> Std-Proposals mailing list
>> Std-Proposals_at_[hidden]
>> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
>>
>

Received on 2025-09-14 15:50:19