why this program is incorrect?

A

Andy

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
 
D

Dietmar Kuehl

Andy said:
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;
 
A

Andreas Andersen

Andy said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top