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.
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
Recommended Posts:
- Find the Deepest Node in a Binary Tree Using Queue STL - SET 2
- C++ program to compare two Strings using Operator Overloading
- not1 and not2 function templates in C++ STL with Examples
- Go vs C++
- Trie Data Structure using smart pointer and OOP in C++
- Modular Exponentiation of Complex Numbers
- std::hash class in C++ STL
- History of C++
- What are Forward declarations in C++
- __builtin_inf() functions of GCC compiler
- Structures in C++
- Variables in C++
- Implementing Forward Iterator in BST
- Count substrings that contain all vowels | SET 2
- Machine Learning in 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.



