
My head is now exploding with understanding the full power and flexibility of multi-index-container, but still it is not clear to me how best to do the simple things! What is the idiomatically 'correct' way to apply the equivalent functionality of the erase-remove idiom to a multi-index-container? I'm assuming that this, (very much abridged for brevity of exposition) multi_index_container mic; mic.erase( remove_if( mic.begin(), mic.end(), pred ), mic.end() ); is doomed since the remove algorithms overwrite removed elements with copied elements which would cause havoc with other indices, and in any case this kind of modification through iterators is explicitly prohibited. I did think of extracting a vector of references, doing erase-remove on that and then applying rearrange(), but it seems rather inelegant. Is there a better way? Thanks. - Rob.

Robert Jones escribió:
My head is now exploding with understanding the full power and flexibility of multi-index-container, but still it is not clear to me how best to do the simple things!
What is the idiomatically 'correct' way to apply the equivalent functionality of the erase-remove idiom to a multi-index-container? [...]
Cannot you just resort to a manual for-loop like this? multi_t mic; ... for(multi_t::iterator it=mic.begin(),it_end=mic.end();it!=it_end;){ if(pred(*it))it=mic.erase(it); else ++it; } Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

On Mon, Sep 15, 2008 at 12:07 PM,
Robert Jones escribió:
My head is now exploding with understanding the full power and flexibility of multi-index-container, but still it is not clear to me how best to do the simple things!
What is the idiomatically 'correct' way to apply the equivalent functionality of the erase-remove idiom to a multi-index-container? [...]
Cannot you just resort to a manual for-loop like this?
multi_t mic; ... for(multi_t::iterator it=mic.begin(),it_end=mic.end();it!=it_end;){ if(pred(*it))it=mic.erase(it); else ++it; }
Yes, but that's also true for the erase-remove idiom in general. I was hoping there was something a bit more slick, ideally a one-liner. Thanks. - Rob.

joaquin@tid.es wrote:
Cannot you just resort to a manual for-loop like this?
multi_t mic; ... for(multi_t::iterator it=mic.begin(),it_end=mic.end();it!=it_end;){ if(pred(*it))it=mic.erase(it); else ++it; }
Btw.: Is this a documented feature that end() will not be invalidated by erase? Sorry, if this is a beginner's question, but I could not find such a statement in the docs (probably too blind). Greetings from Bremen, Daniel

Daniel Krügler escribió:
joaquin@tid.es wrote:
Cannot you just resort to a manual for-loop like this?
multi_t mic; ... for(multi_t::iterator it=mic.begin(),it_end=mic.end();it!=it_end;){ if(pred(*it))it=mic.erase(it); else ++it; }
Btw.: Is this a documented feature that end() will not be invalidated by erase? Sorry, if this is a beginner's question, but I could not find such a statement in the docs (probably too blind).
Yes, see for instance in the case of ordered indices at http://tinyurl.com/5g5kd6 : "Except where noted, ordered indices [...] are models of Sorted Associative Container and Unique Associative Container, much as std::sets are. Accordingly, validity of iterators and references to elements is preserved." Similar statements are made for the rest of index types. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo
participants (3)
-
Daniel Krügler
-
joaquin@tid.es
-
Robert Jones