Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > why this program is incorrect?

Reply
Thread Tools

why this program is incorrect?

 
 
Andy
Guest
Posts: n/a
 
      02-01-2005
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

 
Reply With Quote
 
 
 
 
Andy
Guest
Posts: n/a
 
      02-01-2005
well, it = (queue.rbegin() + 1).base() will work.
andy

 
Reply With Quote
 
 
 
 
Dietmar Kuehl
Guest
Posts: n/a
 
      02-01-2005
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;
--
<private.php?do=newpm&u=> <http://www.dietmar-kuehl.de/>
<http://www.contendix.com> - Software Development & Consulting

 
Reply With Quote
 
Clark S. Cox III
Guest
Posts: n/a
 
      02-01-2005
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


 
Reply With Quote
 
Andreas Andersen
Guest
Posts: n/a
 
      02-02-2005
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
"Why I Program In Ruby (And Maybe Why You Shouldn't)" Trollen Lord Ruby 50 11-29-2007 11:03 AM
why why why why why Mr. SweatyFinger ASP .Net 4 12-21-2006 01:15 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
Cisco 2611 and Cisco 1721 : Why , why , why ????? sam@nospam.org Cisco 10 05-01-2005 08:49 AM
why is my iostream program so much slower than equivalent cstdio program? Mark C++ 20 12-27-2004 10:58 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57