Scoped / Reference Counted Iterator
<p>I'm looking for a way to simulated a "scoped iterator" (for lack of a better phrase). Basically, I want to get an iterator from a collection (map in this case) and wrap it in a "scoped iterator" that would remove the element/iterator from the collection once the last reference was dropped. My current approach is below, but I'm looking for something more elegant.</p>

<pre><code>typedef std::map&lt;int,bool&gt; map_type;
typedef map_type::iterator iter_type;

void iterDelete( std::shared_ptr&lt;map_type&gt; map,  iter_type * iter)
{
    map-&gt;erase(*iter);
    delete(iter);
}    

int main()
{ 
    std::shared_ptr&lt;map_type&gt; myMap( new map_type() ); //std::map because iterators are not invalidated by erase/insert
    iter_type myIter = myMap-&gt;find(7);
    std::shared_ptr&lt;iter_type&gt; scopedIter( new iter_type(myIter), std::bind(iterDelete, myMap, std::placeholders::_1) ); //Deleters keep map in scope until all "scoped iterators" die.
}
</code></pre>
