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 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
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
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
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- How to iterate over the elements of an std::tuple in C++
- How to access private/protected method outside a class in C++
- lvalues references and rvalues references in C++ with Examples
- Implementation of lower_bound() and upper_bound() in Array of Pairs in C++
- Difference between cout and std::cout in C++
- Sorting Vector of Arrays in C++
- C++ program to check whether a String is a Pangram or not
- Difference between pair in Multiset and Multimap in C++ STL
- How to use unordered_map efficiently in C++
- Difference between Array and Map
- Header files in C/C++ with Examples
- C++ Program for the Fractional Knapsack Problem
- Trigraphs in C++ with Examples
- Difference Between DART and C++
- How to fix auto keyword error in Dev-C++
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.

