I have two sorted ranges. I would like to find the first matching element in the ranges, if any.
The operations (on sorted ranges) sections in cppreference do not appear to contain exactly what I want. std::set_intersection is close, but it finds all the matching elements.
Of course I could write my own template algorithm, but it's a bit of a chore to write algorithms up to the STL quality standard.
Another option is to somehow adapt std::set_intersection by using a special output iterator. We could write something like
template <typename T>
struct OneElementIter
{
T &operator*() {
if (full) return dummy;
full = true;
return t;
}
void operator++() {}
bool full = false;
T t;
T dummy;
};
and use it for the output iterator of std::set_intersection. This will waste effort scanning the input ranges past the first match, but it's still O(n) complexity.
However, writing STL-quality iterators is a chore too. I'm not aware of anything in <iterator> that can do this without writing a new iterator class.
If the STL container library included a fixed-size FIFO queue whose push_back is a no-op when full, then we could use a std::back_inserter on a queue of size 1 as the output iterator. But no such container exists.
Is there any elegant way to do this with existing STL tools I'm missing?