473,396 Members | 1,809 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,396 software developers and data experts.

why std::deque::rbegin() - rend() gives a negative number?

Hi,

I don't understand why rbegin() -rend() gives a negative number.

Since rbegin() + 1 gives the one before the last element, I think
rbegin() - rend() should give a positive number.

Thanks,
Peng

#include <deque>
#include <iostream>

int main() {
std::deque<doubleq;
q.push_back(1);
q.push_front(0);
q.push_back(2);
q.push_back(3);
std::cout << q[0] << std::endl;
std::cout << q.rbegin() - q.rend() << std::endl;//gives a negative
number?

std::cout << *(q.rbegin() + 1) << std::endl;// gives the one before
the last element
}
Sep 12 '08 #1
3 2225
On 9ÔÂ12ÈÕ, ÏÂÎç12ʱ06·Ö, Peng Yu <PengYu...@gmail.comwrote:
Hi,

I don't understand why rbegin() -rend() gives a negative number.

Since rbegin() + 1 gives the one before the last element, I think
rbegin() - rend() should give a positive number.

Thanks,
Peng

#include <deque>
#include <iostream>

int main() {
std::deque<doubleq;
q.push_back(1);
q.push_front(0);
q.push_back(2);
q.push_back(3);
std::cout << q[0] << std::endl;
std::cout << q.rbegin() - q.rend() << std::endl;//gives a negative
number?

std::cout << *(q.rbegin() + 1) << std::endl;// gives the one before
the last element

}- Òþ²Ø±»ÒýÓÃÎÄ×Ö -

- ÏÔʾÒýÓõÄÎÄ×Ö -
that's right, rbegin() - rend() gives a negative number indeed
given a vector that have 5 elements,
rbegin() refers to the last element, and rend() refers to the
first element, so
rbegin() + 4 = rend()
then rbegin - rend() = -4
the result(-4) is negative
Sep 12 '08 #2
Peng Yu wrote:
I don't understand why rbegin() -rend() gives a negative number.
Because they return a regular range. rbegin() returns the beginning of
the range and rend() returns the end of the range. rend()-rbegin() (for
random access iterators) returns the size of the range. That's how
iterator ranges are defined.

Just because reverse iterators happen to traverse the container
backwards doesn't change anything. It's still just a regular range, like
any other.

If they behaved like you expect, that could potentially break some
algorithms which expect end-begin to return the proper size of the range.
Sep 12 '08 #3
my fault, as for my example,
rbegin() - rend() should be -5
I thought rend() is the first element.
actually, rend() refer to "null" element before the first element
Sep 13 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

25
by: Jason | last post by:
Hi, below is example code which demonstrates a problem I have encountered. When passing a number to a function I compare it with a string's size and then take certain actions, unfortunately during...
4
by: MikeB | last post by:
Hi, I've observed some odd behaviour in the std::list supplied with our development system. In the accompanying code, the list has at most one string on it, yet the program eventually runs out...
9
by: R.Z. | last post by:
i was wondering whether it pays off in terms of memory use to maintain lots of empty deques (it would be convenient for my algorithms but memory use is more important). and does the size of a deque...
44
by: Josh Mcfarlane | last post by:
Just out of curiosity: When would using std::list be more efficient / effective than using other containers such as vector, deque, etc? As far as I'm aware, list doesn't appear to be...
9
by: toton | last post by:
Hi, I am looking for a circular buffer solution ( a fixed buffer) , where the elements can be pushed back & removed front. It looks stl deque can be a solution. my question is, 1) circular...
7
by: DevNull | last post by:
Hello everyone, I decided to pick c++ back up after not really having used it much in 10 years. Just as a point of refference, there was no standard C++ last time I used regularly. Anyways...
15
by: Juha Nieminen | last post by:
I'm sure this is not a new idea, but I have never heard about it before. I'm wondering if this could work: Assume that you have a common base class and a bunch of classes derived from it, and...
2
by: Heck | last post by:
I borrowed N. Josuttis's code for a queue ("The C++ Standard Library", 1999, p. 450), with which he modifies the interface to read and discard the front element on pop() and to throw an exception...
29
by: NvrBst | last post by:
I've read a bit online seeing that two writes are not safe, which I understand, but would 1 thread push()'ing and 1 thread pop()'ing be thread-safe? Basically my situation is the follows: ...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.