C++ Array Library - end() Function


Advertisements

Description

The C++ function std::array::end() returns an iterator which points to the past-end element of array.

Declaration

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

iterator end() noexcept;
const_iterator end() noexcept;

Parameters

None

Return Value

Returns an iterator pointing to the past-the-end element in the array. This element act as a place-holder and never stores the actual data that is why deferencing this location would result undefined behavior.

If array object is const-qualified, method return const iterator otherwise returns iterator.

Exceptions

This member function never throws exception.

Time complexity

Constant i.e. O(1)

Example

The following example shows the usage of std::array::end() function.

#include <iostream>
#include <array>

using namespace std;

int main(void) {

   array<int, 5> arr = {10, 20, 30, 40, 50};
   /* iterator pointing at the start of array */
   auto start = arr.begin();   
   /* iterator pointing past−the−end of array */
   auto end = arr.end();      
   /* iterate complete array */
   while (start < end) {
      cout << *start << " ";
      ++start;
   }

   cout << endl;

   return 0;
}

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

10 20 30 40 50
array.htm
Advertisements