multi_index_container: replace() modifies my iterator

Hi all, I'm pretty new in boost and I am experimenting an issue I can't understand. It looks like the usage of "replace()" modifies the iterator I am iterating on: Here is a simple example: it iterates properly if I do: Iterator it2, it3; boost::tuples::tie(it2, it3) = m_Container.get< CollectionID
().equal_range( 30 );
while( it2!= it3 ) { 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); comment this line* } ++it2; } whereas if I call "replace()" the iterator value changes to "it3" then it exists the loop: Iterator it2, it3; boost::tuples::tie(it2, it3) = m_Container.get< CollectionID
().equal_range( 30 );
while( it2!= it3 ) { 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);* } ++it2; } the replace() method returns true. I must be missing something but after reading the documentation and some researches on the web I couldn't find the answer. Any help/explanation would be greatly appreciated, Thanks, Nicolas

AMDG On 1/6/2011 12:47 PM, Nicolas Rannou wrote:
I'm pretty new in boost and I am experimenting an issue I can't understand. It looks like the usage of "replace()" modifies the iterator I am iterating on:
Here is a simple example:
it iterates properly if I do:
Iterator it2, it3; boost::tuples::tie(it2, it3) = m_Container.get< CollectionID
().equal_range( 30 );
while( it2!= it3 ) { 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); comment this line* } ++it2; }
whereas if I call "replace()" the iterator value changes to "it3" then it exists the loop: <snip>
the replace() method returns true.
I must be missing something but after reading the documentation and some researches on the web I couldn't find the answer.
Any help/explanation would be greatly appreciated,
The position of the element can change when you change the key. The iterator continues to point to the same element, but the element has moved. In Christ, Steven Watanabe

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 On Jan 6, 2011, at 11:31 PM, Steven Watanabe wrote:
AMDG
On 1/6/2011 12:47 PM, Nicolas Rannou wrote:
I'm pretty new in boost and I am experimenting an issue I can't understand. It looks like the usage of "replace()" modifies the iterator I am iterating on:
Here is a simple example:
it iterates properly if I do:
Iterator it2, it3; boost::tuples::tie(it2, it3) = m_Container.get< CollectionID
().equal_range( 30 );
while( it2!= it3 ) { 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); comment this line* } ++it2; }
whereas if I call "replace()" the iterator value changes to "it3" then it exists the loop: <snip>
the replace() method returns true.
I must be missing something but after reading the documentation and some researches on the web I couldn't find the answer.
Any help/explanation would be greatly appreciated,
The position of the element can change when you change the key. The iterator continues to point to the same element, but the element has moved.
In Christ, Steven Watanabe
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users

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
participants (3)
-
joaquin@tid.es
-
Nicolas Rannou
-
Steven Watanabe