Boost logo

Boost Users :

Subject: Re: [Boost-users] multi_index_container: replace() modifies my iterator
From: Steven Watanabe (watanabesj_at_[hidden])
Date: 2011-01-06 17:31:09


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 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