Hi,
I'm trying to 'reduce' a vector using the erase algorithm within a for loop.
The idea is to scan a table looking for equal successive elements. Once equal elements are found, it erases one of the 2 elements. I'm suspecting a problem in the for loop, due to the iterator. What happens to it's value once an element is deleted ?
void faisceau::reduction(int div)
{
//if (div!=1)
{
std::vector<photon>::iterator iter, itertracker=((*this).tab_photon).begin();
for(iter=itertracker; iter!=((*this).tab_photon).end();iter++)
if ( ((*iter).tps_arrive) == ((*(iter+1)).tps_arrive) )
{
((*iter).ponderation)++;
((*this).tab_photon).erase(iter+1);
itertracker=iter;
}
}
}
I found this on a web:
Because vectors keep an array format, erasing on positions other than the vector end also moves all the elements after the segment erased to their new positions,
This invalidates all iterator and references to elements after position or first.