Topic: Assigning iterator to reverse_iterator ??


Author: Markus Reitz <Markus_Reitz@yahoo.com>
Date: 1999/06/17
Raw View
Hi,

I'm using the STL and have the following problem.

Let L a list of int's - list<int> L;

L consists of several elements and I want to get the maximum-element. So, I
use an iterator to iterate over the list:

list<int>::iterator i;

for(i=L.begin();i!=L.end();i++)
{
  search for the maximum element
  ...
}

And now the problem: When the maximum element is found, i want to generate
to iterators, which point to this entry, one is a "normal" (forward)
iterator and the other a reverse_iterator.

list<int>::iterator Forward;
list<int>::reverse_iterator Backward;

for(i=L.begin();i!=L.end();i++)
{
  file://search

  if found
  {
    Forward=i; file://works fine
    Backward=i; file://doesn't work
    Backward=static_cast<list<int>::reverse_iterator>(i);
      file://works
  }
}

Why do I have to cast here? Is there a way to avoid the cast? If the cast is
unavoidable: is it correct to do it that way?

Thanks

Markus
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: hinnant@_anti-spam_metrowerks.com (Howard Hinnant)
Date: 1999/06/17
Raw View
In article <7kb0eb$j45$1@sun.rhrk.uni-kl.de>, Markus Reitz
<Markus_Reitz@yahoo.com> wrote:

> Let L a list of int's - list<int> L;
>
> L consists of several elements and I want to get the maximum-element. So, I
> use an iterator to iterate over the list:
>
> list<int>::iterator i;
>
> for(i=L.begin();i!=L.end();i++)
> {
>   search for the maximum element
>   ...
> }
>
> And now the problem: When the maximum element is found, i want to generate
> to iterators, which point to this entry, one is a "normal" (forward)
> iterator and the other a reverse_iterator.
>
> list<int>::iterator Forward;
> list<int>::reverse_iterator Backward;
>
> for(i=L.begin();i!=L.end();i++)
> {
>   file://search
>
>   if found
>   {
>     Forward=i; file://works fine
>     Backward=i; file://doesn't work
>     Backward=static_cast<list<int>::reverse_iterator>(i);
>       file://works
>   }
> }
>
> Why do I have to cast here?

Because the reverse_iterator's constructor that takes an iterator is explicit.

>Is there a way to avoid the cast? If the cast is
> unavoidable: is it correct to do it that way?

How about:

#include <algorithm>
#include <list>

int main()
{
   std::list<int> L;
   std::list<int>::iterator Forward = std::max_element(L.begin(), L.end());
   if (Forward != L.end())  // if found
   {
       std::list<int>::reverse_iterator Backward(Forward);
       ...
   }
}


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]