C++ Standard Library C++ STL Library

C++ <array> - end() Function



The C++ array::end function returns the iterator pointing to the past-the-last element of the array container. The past-the-last element of the array is the theoretical element that follows the last element. It does not point to any element, and hence could not be dereferenced.

C++ begin end

Syntax

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

Parameters

No parameter is required.

Return Value

An iterator to the past-the-last element of the sequence container. If the sequence object is constant qualified, the function returns a const_iterator, else returns an iterator.

Time Complexity

Constant i.e, Θ(1).

Example:

In the example below, the array::end function returns the iterator pointing to the past-the-last element of the array MyArray.

#include <iostream>
#include <array>
using namespace std;
 
int main (){
  array<string, 3> MyArray{"Alpha","Coding","Skills"};
  array<string, 3>::iterator it;

  it = MyArray.end();
  it--;
  cout<<*it<<" ";
  it--;
  cout<<*it<<" ";
  it--;
  cout<<*it<<" ";
  return 0;
}

The output of the above code will be:

Skills Coding Alpha

Example:

Lets see another example where the array called MyArray contains integer values and array::end function is used with array::begin function to specify a range including all elements of the array container.

#include <iostream>
#include <array>
using namespace std;
 
int main (){
  array<int, 5> MyArray{10, 20, 30, 40, 50};
  array<int, 5>::iterator it;

  for(it = MyArray.begin(); it != MyArray.end(); ++it)
    cout<<*it<<" ";

  return 0;
}

The output of the above code will be:

10 20 30 40 50 

❮ C++ <array> Library