Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You could use <code>set_intersection</code> and test if the resulting set is empty, but I don't know if that's much faster. </p> <p>The optimal implementation would stop the testing and <code>return false</code> as soon as the first equal element is found. I don't know of any ready-made solution for that, though</p> <pre><code>template&lt;class Set1, class Set2&gt; bool is_disjoint(const Set1 &amp;set1, const Set2 &amp;set2) { Set1::const_iterator it, itEnd = set1.end(); for (it = set1.begin(); it != itEnd; ++it) if (set2.count(*it)) return false; return true; } </code></pre> <p>isn't too complex and should do the job nicely.</p> <p><strong>EDIT:</strong> If you want O(n) performance, use the <em>slightly</em> less compact</p> <pre><code>template&lt;class Set1, class Set2&gt; bool is_disjoint(const Set1 &amp;set1, const Set2 &amp;set2) { Set1::const_iterator it1 = set1.begin(), it1End = set1.end(); if (it1 == it1End) return true; // first set empty =&gt; sets are disjoint Set2::const_iterator it2 = set2.begin(), it2End = set2.end(); if (it2 == it2End) return true; // second set empty =&gt; sets are disjoint // first optimization: check if sets overlap (with O(1) complexity) Set1::const_iterator it1Last = it1End; if (*--it1Last &lt; *it2) return true; // all elements in set1 &lt; all elements in set2 Set2::const_iterator it2Last = it2End; if (*--it2Last &lt; *it1) return true; // all elements in set2 &lt; all elements in set1 // second optimization: begin scanning at the intersection point of the sets it1 = set1.lower_bound(*it2); if (it1 == it1End) return true; it2 = set2.lower_bound(*it1); if (it2 == it2End) return true; // scan the (remaining part of the) sets (with O(n) complexity) for(;;) { if (*it1 &lt; *it2) { if (++it1 == it1End) return true; } else if (*it2 &lt; *it1) { if (++it2 == it2End) return true; } else return false; } } </code></pre> <p>(modified Graphics Noob's modification further, using only operator &lt;)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload