AW: [Insight-users] STL vector iterator

Luis Ibanez luis.ibanez at kitware.com
Wed Aug 11 12:54:07 EDT 2004


Hi Zein,

Strange...

Please post the values of size() that you
get before and after you call erase().

A common mistake is to do consecutive erases
and use invalid iterators as arguments for one
of the erase() invokations. Note that after you
call erase, all the iterators posterior to the
range that you erased become invalid.

Erase returns an iterator pointing to the
next valid iterator after removing the
ones in the range.

You may want to do something like:

   Iterator next_valid;
   next_valid = contour.erase( first, last );



Regards,


    Luis



---------------
salah wrote:

> Thanks Frederic,
> 
> one more question please.
> 
> I am calling the erase function to erase the elements of the vector
> between these two iterators as follows:
> 
>    std::cout << "Contour length BEFORE cutting = " << contour.size() << std::endl;
>    contour.erase(startIter, endIter);
>    std::cout << "Contour length AFTER cutting = " << contour.size() << std::endl;
> 
> The strange thing is that the printed length of the counter after erasing 
> elements is greater than before!
> 
> Could you guess what is going on?
> 
> Many thanks,
> 
> Zein
> 
> 
> 
> 
>>-----Ursprüngliche Nachricht-----
>>Von: FREDERICPCX [mailto:FREDERICPCX at terra.es]
>>Gesendet: Wednesday, August 11, 2004 4:57 PM
>>An: salah
>>Cc: insight-users at itk.org
>>Betreff: Re: [Insight-users] STL vector iterator
>>
>>
>>
>>Hello Zein,
>>
>>if your vector is called v, maybe you can simply use something like
>>the following:
>>
>>if (distance(v.begin(), iter1) < distance(v.begin(), iter2) { ...
>>
>>Beware that calling some methods on the STL containers can invalidate
>>their related iterators. More in Chapter 23 of the 1997 C++ Public
>>Review Document: http://anubis.dkuug.dk/jtc1/sc22/open/n2356/
>>
>>Hope this helps,
>>
>>Frederic
>>
>>----- Mensaje Original -----
>>De: "salah" <salah at gris.uni-tuebingen.de>
>>Fecha: Miercoles, Agosto 11, 2004 4:38 pm
>>Asunto: [Insight-users] STL vector iterator
>>
>>
>>>Hello,
>>>
>>>I am using two iterators to point to two different 
>>
>>positions on an STL
>>
>>>vector. I want to decide which iterator is advancing more that the 
>>>other.is it meaningful to just compare the two iterator? i.e. 
>>>something like this
>>>
>>>  typedef std::vector< ...... > VectorType;
>>>  VectorType::iterator  iter1, iter2;
>>>  ....
>>>  ....
>>>  if (iter1  < iter2) {
>>>     ....
>>>     ....
>>>  } 
>>>
>>>or something else is needed. This comparison does not seem 
>>>to work by me.
>>>
>>>Thanks,
>>>Zein
>>>_______________________________________________
>>>Insight-users mailing list
>>>Insight-users at itk.org
>>>http://www.itk.org/mailman/listinfo/insight-users
>>>
>>
>>
>>
> _______________________________________________
> Insight-users mailing list
> Insight-users at itk.org
> http://www.itk.org/mailman/listinfo/insight-users
> 






More information about the Insight-users mailing list