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

28 Sep, 2021
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. 
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 Exceptions1. If the array container is empty, it causes undefined behavior. 2. It has a no exception throw guarantee if the array is not empty.
C++
// 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
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 Exceptions1. If the array container is empty, it causes undefined behavior. 2. It has a no exception throw guarantee if the array is not empty.
C++
// 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
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.
C++
// 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
 
chhabradhanvi
cpp-array
CPP-Library
STL
C++
STL
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Operator Overloading in C++
Polymorphism in C++
Sorting a vector in C++
Friend class and function in C++
std::string class in C++
Pair in C++ Standard Template Library (STL)
Queue in C++ Standard Template Library (STL)
Inline Functions in C++
Array of Strings in C++ (5 Different Ways to Create)
Convert string to char array in C++