Boost logo

Boost Users :

Subject: Re: [Boost-users] multi_index_container: replace() modifies my iterator
From: joaquin_at_[hidden]
Date: 2011-01-10 04:21:26


Nicolas Rannou escribió:
> Thanks, that is what I was suspecting.
>
> Is there already something in boost to do what I intend to do?
> If not, which strategy would you recommend to by pass this issue?
> I got some ideas but nothing straight forward and I would like it to
> be as efficient as possible.
>
> Thanks again,
>
> Nicolas
>

In this particular case, you can do the following:

    Iterator it2 = m_Container.get< CollectionID >().lower_bound( 30 );

    while( it2!=m_Container.get< CollectionID >().end() &&
it2->CollectionID==30)
      {
      int time = it2->TCoord;
      if( time < 500 ) // we want to change the collection ID in this case
        {
        Structure tempStructure(*it2);
        tempStructure.CollectionID = 40;
        m_Container.get< CollectionID >().replace(it2++, tempStructure);
        }
      else ++it2;
      }

I haven't compiled it, but I think it should work. Two notes about the code:

1. Note that in the invocation to replace() we do a post-increment of
it2, so that
it2 points to the following element before the current one is replaced and,
consequently, moved to a new position.
2. The while condition of the loop has changed: as a result of replacements,
it could be the case that modified elements get between the range of
elements with
CollectionID==30 and it3, so checking for it2!=it3 is not safe.

HTH,

Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo

Este mensaje se dirige exclusivamente a su destinatario. Puede consultar nuestra política de envío y recepción de correo electrónico en el enlace situado más abajo.
This message is intended exclusively for its addressee. We only send and receive email on the basis of the terms set out at.
http://www.tid.es/ES/PAGINAS/disclaimer.aspx


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net