why this program is incorrect?

Discussion in 'C++' started by Andy, Feb 1, 2005.

  1. Andy

    Andy Guest

    Hi,

    I am trying to get the iterator that points to the last element of a
    deque.
    However, the following program is incorrect:

    #include <deque>
    #include <iostream>
    using namespace std;

    int main()
    {
    deque<int> queue;
    deque<int>::iterator it;

    queue.clear();
    queue.push_back(5);
    it = queue.rbegin().base();

    cout<<"*it = "<<*it<<endl;
    return 0;
    }

    How can I get a correct one?
    Thanks a lot!

    Andy
     
    Andy, Feb 1, 2005
    #1
    1. Advertising

  2. Andy

    Andy Guest

    well, it = (queue.rbegin() + 1).base() will work.
    andy
     
    Andy, Feb 1, 2005
    #2
    1. Advertising

  3. Andy wrote:
    > I am trying to get the iterator that points to the last element of a
    > deque.


    /**/ std::deque<int> queue;
    /**/ queue.push_back(5);
    /**/ std::deque<int>::iterator it = queue.end() - 1;
    --
    <mailto:> <http://www.dietmar-kuehl.de/>
    <http://www.contendix.com> - Software Development & Consulting
     
    Dietmar Kuehl, Feb 1, 2005
    #3
  4. On 2005-02-01 09:39:31 -0500, "Andy" <> said:

    > well, it = (queue.rbegin() + 1).base() will work.
    > andy


    Why even bother with reverse iterators? Why not just use:

    it = queue.end() - 1;

    --
    Clark S. Cox, III
     
    Clark S. Cox III, Feb 1, 2005
    #4
  5. Andy wrote:
    > Hi,
    >
    > I am trying to get the iterator that points to the last element of a
    > deque.
    > However, the following program is incorrect:
    >
    > #include <deque>
    > #include <iostream>
    > using namespace std;
    >
    > int main()
    > {
    > deque<int> queue;
    > deque<int>::iterator it;
    >
    > queue.clear();
    > queue.push_back(5);
    > it = queue.rbegin().base();
    >
    > cout<<"*it = "<<*it<<endl;
    > return 0;
    > }
    >
    > How can I get a correct one?
    > Thanks a lot!
    >
    > Andy
    >


    Hi,

    base() returns an iterator one beyond the element pointed to by the
    corresponding reverse_iterator so you need

    it = --(queue.rbegin().base());

    instead.

    /Andreas
     
    Andreas Andersen, Feb 2, 2005
    #5
    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. Curzio Basso
    Replies:
    4
    Views:
    350
    Tim Hochberg
    Mar 4, 2004
  2. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    909
    Mark Rae
    Dec 21, 2006
  3. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,996
    Smokey Grindel
    Dec 2, 2006
  4. piruk
    Replies:
    1
    Views:
    388
    Ian Collins
    Apr 1, 2007
  5. Udo Grabowski
    Replies:
    4
    Views:
    157
    Udo Grabowski
    Sep 3, 2009
Loading...

Share This Page