array::front() and array::back() in C++ STL
Last Updated :
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.
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 behavior.
2. It has a no exception throw guarantee if the array is not empty.
C++
#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 behavior.
2. It has a no exception throw guarantee if the array is not empty.
C++
#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.
C++
#include <array>
#include <iostream>
using namespace std;
int main()
{
array< int , 8> myarray{ 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
Similar Reads
STD::array in C++
The array is a collection of homogeneous objects and this array container is defined for constant size arrays or (static size). This container wraps around fixed-size arrays and the information of its size are not lost when declared to a pointer. In order to utilize arrays, we need to include the ar
5 min read
array::begin() and array::end() 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::begin() begin() function is used to return an iterator pointing to the first element of the array containe
3 min read
array::size() in C++ STL
The array::size() method is used to find the number of elements in the array container. It is the member method std::array class defined inside <array> header file. In this article, we will learn about the array::size() method in C++. Example: [GFGTABS] C++ // C++ Program to illustrate the use
2 min read
array::empty() 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::empty() empty() function is used to check if the array container is empty or not. Syntax : arrayname.empty
1 min read
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
3 min read
array::at() 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::at() This function is used to return the reference to the element present at the position given as the par
2 min read
array::operator[ ] 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::operator[] This operator is used to reference the element present at position given inside the operator.
2 min read
array get() function in C++ STL
The array::get() is a built-in function in C++ STL which returns a reference to the i-th element of the array container. Syntax: get[Tex][/Tex](array_name) Parameters: The function accepts two mandatory parameters which are described below. i - position of an element in the array, with 0 as the posi
2 min read
array data() in C++ STL with Examples
The array::data() is a built-in function in C++ STL which returns an pointer pointing to the first element in the array object. Syntax: array_name.data() Parameters: The function does not accept any parameters. Return Value: The function returns an pointer. Below programs illustrate the above functi
2 min read
array::max_size() 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::max_size() This function returns the maximum number of elements that the array container can contain. In c
1 min read