C++ <array> - back() Function
The C++ array::back function returns a reference to the last element of the array. Please note that, Unlike the array::rbegin function, which returns the reverse iterator pointing to the last element, it returns the a direct reference to the same element of the array.
Syntax
reference back(); const_reference back() const;
Parameters
No parameter is required.
Return Value
A reference to the last element of the array.
Time Complexity
Constant i.e, Θ(1).
Example:
In the example below, the array::back function is used to access the last element of the array Arr.
#include <iostream> #include <array> using namespace std; int main (){ array<int, 5> Arr{10, 20, 30, 40, 50}; cout<<"The last element of Arr is: "; cout<<Arr.back(); //Adding 100 to the last element of the Arr Arr.back() = Arr.back() + 100; cout<<"\nThe last element of Arr is: "; cout<<Arr.back(); return 0; }
The output of the above code will be:
The last element of Arr is: 50 The last element of Arr is: 150
❮ C++ <array> Library