title: C++ iterating through a set
body: <p>I recently changed some code to use a set instead of a vector:</p>
  
  <pre><code>std::set&lt;b2Body *&gt;toDestroy;
  //std::vector&lt;b2Body *&gt;toDestroy;
  </code></pre>
  
  <p>But now I'm not sure how to iterate the set to find objects. This is what I had:</p>
  
  <pre><code>std::vector&lt;b2Body *&gt;::iterator pos2;
      for(pos2 = toDestroy.begin(); pos2 != toDestroy.end(); ++pos2) {
          b2Body *body = *pos2;     
          if (body-&gt;GetUserData() != NULL) {
              CCSprite *sprite = (CCSprite *) body-&gt;GetUserData();
              [self removeChild:sprite cleanup:YES];
          }
          _world-&gt;DestroyBody(body);
      }
  </code></pre>
  
  <p>What is the equivalent now that toDestroy is a set? Coming from Objective-C so I'm just learning best practices in C++.</p>
  
  <p>EDIT: adding the error message I get:</p>
  
  <pre><code>error: no match for 'operator=' in 'pos2 = toDestroy. std::set&lt;_Key, _Compare, _Alloc&gt;::begin [with _Key = b2Body*, _Compare = std::less&lt;b2Body*&gt;, _Alloc = std::allocator&lt;b2Body*&gt;]()'
  </code></pre>