C++ Array Library - crbegin() Function


Advertisements

Description

The C++ function std::array::crbegin() returns a constant reverse iterator pointing to the last element of the array.

Declaration

Following is the declaration for std::array::crbegin() function form std::array header.

const_reverse_iterator crbegin() const noexcept;

Parameters

None

Return Value

Returns a constant reverse iterator which points to the last element of the array. Iterator return by this method is just a right before element which would be pointed by member function end. In other words it returns a constant reverse iterator which points to the first element of the reversed array container.

Exceptions

This member function never throws exception.

Time complexity

Constant i.e. O(1)

Example

Let us see how to use reverse iterator to print array contents reverse order.

#include <iostream>
#include <array>

using namespace std;

int main(void) {

   array<int, 5> arr = {10, 20, 30, 40, 50};

   /* We can only iterate container by using it */
   /* Any attemp to modify value pointed by iterator will cause compliation error */
   for (auto it = arr.crbegin(); it != arr.crend(); ++it)
      cout << *it << " ";

   cout << endl;

   return 0;

}

Let us compile and run the above program, this will produce the following result −

50 40 30 20 10
array.htm
Advertisements