473,379 Members | 1,190 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

why this program is incorrect?

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
4 1123
well, it = (queue.rbegin() + 1).base() will work.
andy

Jul 23 '05 #2
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: muser | last post by:
Karl this is albeit your program, in the program that you wrote for me I learned quite alot, i didn't realise I could return NULL for instance or even use functions without first declaring them,...
3
by: Ralph Purtcher-Wydenbruck | last post by:
This is a worrying bug, and indicates a potential problem in the Framework CLR. This bug affected a project I was working on, which behaved differently when run from the Visual Studio IDE than when...
7
by: ibtc209 | last post by:
I just started programming in C, and I need some help with this problem. Your program will read the information about one MiniPoker hand, namely the rank and suit of the hand’s first card, and...
2
by: gilllyo | last post by:
I'm trying to run a program and pass it arguments. I know that the arguments provided are correct but I get a "The Parameter is incorrect" error. I has used the exact same arguments as provided in...
5
by: Lloyd Dupont | last post by:
Hi All! I have this application of mine which works well. It even used to work on Vista. (I work on XP most of the time). There are some ManagedC++ assemblies in the project. Now when I try...
3
by: Ron Jackson | last post by:
I am using Python 2.5 on Windows XP. I have installed Pyserial and win32all extensions. When I try to run the example program scan.py (included below), or any other program using pyserial, as...
3
by: kaka_hunter | last post by:
#include <iostream> #include <fstream> using namespace std; const int max_tries=7; int earnings=0; int wordnum; void getword () { ifstream fin;
20
by: Robbie Hatley | last post by:
I needed a quick program called "random" that gives a random positive integer from n1 to n2. For example, if I type "random 38, 43", I want the program to print a random member of the set {38,...
6
by: priyajohal | last post by:
#include<fstream.h> #include<process.h> #include<stdlib.h> #include<conio.h> #include<string.h> #include<dos.h> #include<ctype.h> #include<stdio.h> void setup() void help();
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.