Skip to content
Related Articles

Related Articles

Improve Article
array::front() and array::back() in C++ STL
  • Difficulty Level : Basic
  • Last Updated : 22 Dec, 2017


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.






// 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;
}

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.




// 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;
}

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.




// 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";
}

Output:

7
Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.



My Personal Notes arrow_drop_up
Recommended Articles
Page :