Facebook Page Twitter Page LinkedIn Page
× C++ Standard Library C++ STL Library


The C++ array::front function is used to return a reference to the first element of the array. Please note that, Unlike the array::begin function, which returns the iterator pointing to the first element, it returns the a direct reference to the same element of the array.

Syntax

reference front();
const_reference front() const;

Parameters

No parameter is required.

Return Value

A reference to the first element of the array.

Time Complexity

Constant i.e, Θ(1).

Example:

In the below example, the array::front function is used to access the first element of the array MyArray.

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

  cout<<"The first element of MyArray is: ";
  cout<<MyArray.front();

  cout<<"\n\nAdd 100 to the first element of the MyArray.\n\n";
  MyArray.front() = MyArray.front() + 100;

  cout<<"Now, The first element of MyArray is: ";
  cout<<MyArray.front();  
  return 0;
}

The output of the above code will be:

The first element of MyArray is: 10

Add 100 to the first element of the MyArray.

Now, The first element of MyArray is: 110

❮ C++ <array> Library