array::front() and array::back() in C++ STL


Array classes are generally more efficient, light-weight and reliable than C-style arrays. The introduction of array class from C++11 has offered a better alternative for C-style arrays.

array::front()

This function is used to reference the first element of the array container. This function can be used to fetch the first element of an array.
Syntax :

arrayname.front()
Parameters :
No parameters are passed.
Returns :
Direct reference to the first element of the array container.

Examples:

Input :  myarray = {1, 2, 3, 4}
         myarray.front()
Output : 1

Input :  myarray = {3, 6, 2, 8}
         myarray.front()
Output : 3

Errors and Exceptions

1. If the array container is empty, it causes undefined behaviour.
2. It has a no exception throw guarantee if the array is not empty.

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to illustrate
// Implementation of front() function
#include <array>
#include <iostream>
using namespace std;
  
int main()
{
    array<int, 5> myarray{ 1, 2, 3, 4, 5 };
    cout << myarray.front();
    return 0;
}

chevron_right


Output:

1
array::back()

This function is used to reference the last element of the array container. This function can be used to fetch the last element from the end of a array.
Syntax :

arrayname.back()
Parameters :
No parameters are passed.
Returns :
Direct reference to the last element of the array container.

Examples:

Input :  myarray = {1, 2, 3, 4}
         myarray.back()
Output : 4

Input :  myarray = {4, 5, 6, 7}
         myarray.back()
Output : 7

Errors and Exceptions

1. If the array container is empty, it causes undefined behaviour.
2. It has a no exception throw guarantee if the array is not empty.

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to illustrate
// Implementation of back() function
#include <array>
#include <iostream>
using namespace std;
  
int main()
{
    array<int, 5> myarray{ 1, 2, 3, 4, 5 };
    cout << myarray.back();
    return 0;
}

chevron_right


Output:

5

Difference between front(), back() and begin, end() function

begin() and end() function return an iterator(like a pointer) initialized to the first or the last element of the container that can be used to iterate through the collection, while front() and back() function just return a reference to the first or the last element of the container.

Application
Given an array of integers, print the difference between the first and the last element.

Input: 1, 2, 3, 4, 5, 6, 7, 8
Output:7
Explanation: Last element = 8, First element = 1, Difference = 7

Algorithm
1. Compare the first and the last element.
2. If first element is larger, subtract last element from it and print it.
3. Else subtract first element from the last element and print it.

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to illustrate
// application Of front() and back() function
#include <array>
#include <iostream>
using namespace std;
  
int main()
{
    array<int, 8> myarray{ 1, 2, 3, 4, 5, 6, 7, 8 };
  
    // Array becomes 1, 2, 3, 4, 5, 6, 7, 8
  
    if (myarray.front() > myarray.back()) {
        cout << myarray.front() - myarray.back();
    }
    else if (myarray.front() < myarray.back()) {
        cout << myarray.back() - myarray.front();
    }
    else
        cout << "0";
}

chevron_right


Output:

7


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.