By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,897 Members | 1,969 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,897 IT Pros & Developers. It's quick & easy.

why this program is incorrect?

P: n/a
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

Jul 23 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
well, it = (queue.rbegin() + 1).base() will work.
andy

Jul 23 '05 #2

P: n/a
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:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.contendix.com> - Software Development & Consulting

Jul 23 '05 #3

P: n/a
On 2005-02-01 09:39:31 -0500, "Andy" <gn***@yahoo.com> 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
cl*******@gmail.com

Jul 23 '05 #4

P: n/a
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

Jul 23 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.