Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Since <code>std::set</code> is a sorted container, you can compare the set boundaries to see if they possibly intersect, and if so, do the expensive STL operation.</p> <h3>Edit:</h3> <p>Here's where clam fishing gets serious ...<br><br> All the code posted so far tries to re-implement what's already in &lt;algorithm&gt;. Here's the gist of <code>set_intersection</code>:</p> <pre><code> template&lt;typename _InputIterator1, typename _InputIterator2, typename _OutputIterator&gt; _OutputIterator set_intersection(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result) { while (__first1 != __last1 && __first2 != __last2) if (*__first1 &lt; *__first2) ++__first1; else if (*__first2 &lt; *__first1) ++__first2; else { *__result = *__first1; ++__first1; ++__first2; ++__result; } return __result; } </code></pre> <p>Pretty optimal already except for the assignment to the output iterator, which is unnecessary for finding just the fact whether two sets are joint. This can be used though to flip a flag as follows:</p> <pre><code> /// fake insert container template &lt;typename T&gt; struct intersect_flag { typedef int iterator; typedef typename T::const_reference const_reference; bool flag_; // tells whether given sets intersect intersect_flag() : flag_( false ) {} iterator insert( iterator, const_reference ) { flag_ = true; return 0; } }; // ... typedef std::set&lt;std::string&gt; my_set; my_set s0, s1; intersect_flag&lt;my_set&gt; intf; // ... std::set_intersection( s0.begin(), s0.end(), s1.begin(), s1.end(), std::inserter( intf, 0 )); if ( intf.flag_ ) // sets intersect { // ... </code></pre> <p>This avoids copying the objects from the original sets and lets you reuse STL algorithms.</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