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

P

Peng Yu

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<double> q;
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
}
 
Z

zhangyw80

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<double> q;
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
 
J

Juha Nieminen

Peng said:
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.
 
J

James Kanze

Do you have to increment or decrement rbegin() to get to rend()?

rbegin() and rend() return iterators. The difference between
iterators is the number of times you have to increment
(positive difference) or decrement (negative difference) the
first iterator to get to the second. Iterators don't
(necessarily) designate elements in a container; they designate
elements in a sequence. In the case of reverse iterators, the
sequence has the opposite order as the sequence of the
underlying iterators.
#include <deque>
#include <iostream>
int main() {
std::deque<double> q;
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()

If the vector has five elements, that must be rbegin() + 5 ==
rend(). (His vector only has four elements.)
then rbegin - rend() = -4
the result(-4) is negative

The results of rend() - rbegin() should be the number of
elements in the vector. The results of rbegin() - rend() should
be the negation of this.
 
Z

zhangyw80

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
 

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

Forum statistics

Threads
473,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top