Iterators and reverse iterators

Discussion in 'C++' started by Marcin Kaliciñski, May 8, 2005.

  1. template<class RanAccIt>
    void some_algorithm(RanAccIt begin, RanAccIt end)
    {
    // this algorithm involves calling std::lexicographical_compare
    // on range [begin, end), and on reverse of this range
    // (i.e. as rbegin, rend was passed)
    }

    How can I call lexicographical_compare inside the function so that it
    traverses the range backwards?

    The ideal would be to somehow make reverse iterators from normal iterators,
    but I can't see a way to do it (even though the iterators are random access,
    so backwards iteration is fully supported).

    The only way I see is to pass reverse iterators to the function in addition
    to normal iterators. I don't want to do that, because:
    (1) it is redundant - begin/end define the range perfectly
    (2) it reveals internals of the algorithm to the outside - one day I will
    come with an implementation that does not need reverse iterators, and I will
    have to change function signature

    Is there other way?

    Also, if I wanted to hand-code a loop over reverse of range [begin, end),
    would the below be conforming:

    template<class RanAccIt>
    void some_algorithm(RanAccIt begin, RanAccIt end)
    {
    RanAccIt rbegin = end - 1;
    RanAccIt rend = begin - 1;
    for (RanAccIt it = rbegin; it != rend; --it)
    {
    /* ... */
    }
    }

    cheers,
    M.
     
    Marcin Kaliciñski, May 8, 2005
    #1
    1. Advertising

  2. Marcin Kaliciñski

    Kai-Uwe Bux Guest

    Marcin Kaliciñski wrote:

    > template<class RanAccIt>
    > void some_algorithm(RanAccIt begin, RanAccIt end)
    > {
    > // this algorithm involves calling std::lexicographical_compare
    > // on range [begin, end), and on reverse of this range
    > // (i.e. as rbegin, rend was passed)
    > }
    >
    > How can I call lexicographical_compare inside the function so that it
    > traverses the range backwards?
    >
    > The ideal would be to somehow make reverse iterators from normal
    > iterators, but I can't see a way to do it (even though the iterators are
    > random access, so backwards iteration is fully supported).
    >
    > The only way I see is to pass reverse iterators to the function in
    > addition to normal iterators. I don't want to do that, because:
    > (1) it is redundant - begin/end define the range perfectly
    > (2) it reveals internals of the algorithm to the outside - one day I will
    > come with an implementation that does not need reverse iterators, and I
    > will have to change function signature
    >
    > Is there other way?


    You want to have a look at std::reverse_iterator:


    #include <iterator>
    #include <iostream>

    template <typename RndAccIt>
    void print_reversed_range ( std::eek:stream & out,
    RndAccIt const & from,
    RndAccIt const & to ) {
    for ( std::reverse_iterator< RndAccIt > iter ( to );
    iter != std::reverse_iterator< RndAccIt >( from );
    ++ iter ) {
    out << *iter << ' ';
    }
    }

    #include <vector>

    int main ( void ) {
    std::vector< int > i_vect;
    for ( int i = 0; i < 10; ++i ) {
    i_vect.push_back( i );
    }
    print_reversed_range( std::cout, i_vect.begin(), i_vect.end() );
    }


    > Also, if I wanted to hand-code a loop over reverse of range [begin, end),
    > would the below be conforming:
    >
    > template<class RanAccIt>
    > void some_algorithm(RanAccIt begin, RanAccIt end)
    > {
    > RanAccIt rbegin = end - 1;
    > RanAccIt rend = begin - 1;


    Ouch, the before-begin position is in general not a valid thing.

    > for (RanAccIt it = rbegin; it != rend; --it)
    > {
    > /* ... */
    > }
    > }
    >
    > cheers,
    > M.



    Best

    Kai-Uwe Bux
     
    Kai-Uwe Bux, May 8, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. dogbite
    Replies:
    4
    Views:
    694
    osmium
    Oct 10, 2003
  2. Alexander Stippler

    reverse iterators revisited

    Alexander Stippler, Jan 1, 2004, in forum: C++
    Replies:
    0
    Views:
    369
    Alexander Stippler
    Jan 1, 2004
  3. Christopher

    reverse iterators and erasing

    Christopher, Feb 25, 2009, in forum: C++
    Replies:
    1
    Views:
    318
    Christopher
    Feb 26, 2009
  4. , India
    Replies:
    10
    Views:
    1,084
    James Kanze
    Aug 8, 2009
  5. Rahul Apte
    Replies:
    0
    Views:
    544
    Rahul Apte
    Apr 27, 2004
Loading...

Share This Page