C++ List back
The back function in C++ List retrieves the final element of the list, offering direct access to it.
Difference between back and end function
The end method provides an iterator pointing to the element, whereas the back function gives a direct reference to that specific element.
Syntax
reference back();
Parameter
It does not contain any parameter.
Return value
It provides the explicit pointer to the final element.
Example
Let's see a simple example
#include <iostream>
#include<list>
using namespace std;
int main()
{
std::list<char> li={'+','-','*','@'};
std::cout <<"back() is :"<< li.back() << std::endl;
return 0;
}
Output:
back() is : @
In this instance, the back method retrieves the final element of the collection, which is the symbol @.