Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check for empty intersection in STL

Tags:

c++

set

stl

How do I check for the empty intersection of two std::sets? I can use set_intersection, but that's unnecessarily slow, I need only bool answer.

Remark: std::set means ordered sets, they are of the same type etc.

like image 386
yo' Avatar asked Oct 17 '12 18:10

yo'


1 Answers

Anything wrong with just coding it yourself?

bool empty_intersection(const set<int>& x, const set<int>& y)
{
    std<int>::const_iterator i = x.begin();
    std<int>::const_iterator j = y.begin();
    while (i != x.end() && j != y.end())
    {
      if (*i == *j)
        return false;
      else if (*i < *j)
        ++i;
      else
        ++j;
    }
    return true;
}

Something like that anyway. Completely untested code.

like image 87
john Avatar answered Sep 20 '22 12:09

john