Quốc Cường
Quốc Cường

Reputation: 495

What is the difference between begin () and rend ()?

I have a question in iterators about the difference between begin() and rend().

#include <iostream>
#include <array>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
    vector<int> v1;
    v1 = {9,2,6,4,5};
    cout<<*v1.begin();
    cout<<*v1.rend();
    return 0;
}

cout<<*v1.begin(); returns 9

but cout<<*v1.rend(); returns a number that is not 9

Why are there such different results?

Upvotes: 3

Views: 3841

Answers (4)

vector::rend() is a built-in function in the C++ standard library which returns a reverse iterator pointing to the theoretical element right before the first element in the array container. but vector::begin() returns an iterator pointing to the first element in the vector.

See this code :

for (auto it = v1.rbegin(); it != v1.rend(); it++) 
    cout << *it << " ";

The vector elements in reverse order are :

5 4 6 2 9

To iterate in the vector always choose one of these methods together :

  • vector::begin() and vector::end()
  • vector::cbegin() and vector::cend()
  • vector::crbegin() and vector::crend()
  • vector::rbegin() and vector::rend()

For more information see "C++ Vector Tutorial With Example" by Ankit Lathiya.

Try it online

Upvotes: 3

Alan Birtles
Alan Birtles

Reputation: 36488

Dereferencing end() or rend() has undefined behaviour.

begin() points to the first element, rbegin() points to the last element. end() (in most cases) points to one after the last element and rend() effectively points to one before the first element (though it isn't implemented like that).

Upvotes: 7

eerorika
eerorika

Reputation: 238411

What is the difference between begin () and rend ()?

begin returns an iterator to the first element of the container.

rend returns a reverse iterator to one before the first element of the container (which is one past the last element in the reverse iterator range).

*v1.rend()

The behaviour of indirecting through the rend iterator is undefined (same goes for indirecting through end iterator).

Why are there such different results?

Besides behaviour being undefined in one case and not the other, since they refer to different to different elements (one of them being an element that doesn't exist), there is little reason to assume the results to be the same.

Upvotes: 5

templatetypedef
templatetypedef

Reputation: 373032

In C++, ranges are marked by a pair of iterators marking the beginning of the range and a position one past the end of the range. For containers, the begin() and end() member functions provide you with a pair of iterators to the first and past-the-end positions. It’s not safe to read from end(), since it doesn’t point to an actual element.

Similarly, the rbegin() and rend() member functions return reverse iterators that point, respectively, to the last and just-before-the-first positions. For the same reason that it’s not safe to dereference the end() iterator (it’s past the end of the range), you shouldn’t dereference the rend() iterator, since it doesn’t point to an element within the container.

Upvotes: 10

Related Questions