[Dcmlib] Re: Reverse Iterators vs std::sort : answer to my question

Jean-Pierre Roux jpr at creatis.insa-lyon.fr
Wed Aug 31 11:59:22 CEST 2005


Jean-Pierre Roux wrote:

> Hi ! Reverse iterators are well known (?) and work perfectly :
>
> ------------------------------------------------------------------
> Reverse iterators are bidirectional iterators that invert the meaning 
> of operators +, -, ++ and --. Thus, incrementing a reverse iterator 
> moves it one position /backwards/, whereas decrementing it causes it 
> to move to the /next/ element. The rbegin() and rend() member 
> functions return reverse iterators:
>
>void print_backwards(vector<int> &vi)
>{
> vector<int>::reverse_iterator rit=vi.rbegin();
> while (rit<vi.rend())
> {
> cout<<*rit<<endl;
> ++rit; //move one position backwards
> }
>}
>-----------------------------------------------
>  
>
> My question :
> How can we use them (or anything else) to *reverse* std::sort a 
> std::vector (or any std::sortable object) ?
>

==> Definitively (?) there is no way to reverse sort a std object 
(probabely for some good reasons)
A std::reverse() method exists, but doesn't deal with the order.
Just use something like :

   std::sort(fileList->begin(), fileList->end(), 
userSuppliedLessThanFunction);
   if (!DirectOrder)
   {
      std::reverse(fileList->begin(), fileList->end());
   }

Thx to Benoit for telling.
JPRx

> What I wouldn't like to write is something like the following stuff :
>
>    if (DirectOrder)
>       std::sort(fileList->begin(), fileList->end(), 
> userSuppliedLessThanFunction);
>    else
>       std::sort(fileList->begin(), fileList->end(), 
> userSuppliedGreaterThanFunction);
>
> Thx
>
> JPRx
>
>------------------------------------------------------------------------
>
>_______________________________________________
>Dcmlib mailing list
>Dcmlib at creatis.insa-lyon.fr
>http://www.creatis.insa-lyon.fr/mailman/listinfo/dcmlib
>



More information about the Dcmlib mailing list