C++ List::back() Function



The C++ std::list::back() function is used to retrieve the reference to the last element in the list.

In C++, a reference is a value that enables a program to indirectly access a particular data, such as a variable or record. If the current container(or list) is non-empty, the expression list.back() is equivalent to *std::prev(list.end()). The back()function returns zero if the current list(type int) is empty.

Calling the back() function on an empty list causes undefined behavior.

Syntax

Following is the syntax of the C++ std::list::back() function −

reference back();   

Parameters

  • It does not accepts any parameter.

Return value

This function returns the last the reference of the last element in the list.

Example 1

In the following program, we are using the std::list::back() function to display the last element of the current list {1,2,3,4,5}.

#include<iostream>
#include<list>
using namespace std;

int main() {
   //create an integer list
   list<int> l = {1, 2, 3, 4, 5};
   cout<<"The list elements are: ";
   for(int lst : l){
      cout<<lst<<" ";
   }
   cout<<"\nLast element of the list is: "<<l.back()<< endl;
   return 0;
}

Output

On executing the above program, it will produce the following output −

The list elements are: 1 2 3 4 5 
Last element of the list is: 5

Example 2

Following is another example of the std::list::back() function, here we are creating a list (type char) named char_list with the values {'a','b','c','d','e'}. Then, using the back() function, we are trying to retrieve the last element of the current list.

#include <iostream>
#include <list>
using namespace std;

int main() {
   //create a character list
   list<char>char_list = {'a','b','c','d','e'};
   cout<<"The list elements are: ";
   for(char lst : char_list){
      cout<<lst<<" ";
   }
   cout<<"\nLast element of the list is: "<<char_list.back()<< endl;
   return 0;
}

Output

Following is the output of the above program −

The list elements are: a b c d e 
Last element of the list is: e

Example 3

If the current list is a string type.

The following program displays the last element of the current list(type string) {'Aman', 'Rohit', 'Rahul', 'Reeta'}.

#include <iostream>
#include <list>
#include<string>
using namespace std;

int main() {
   list<string> Name;
   Name.push_back("Aman");
   Name.push_back("Rohit");
   Name.push_back("Rahul");
   Name.push_back("Reeta");
   cout<<"The list elements are: ";
   for(string s: Name){
      cout<<s<<" ";
   }
   string name = Name.front();
   cout <<"\nThe first element in list is: "<<name;
   return 0;
}

Output

The above program generates the output as follows −

The list elements are: Aman Rohit Rahul Reeta 
The first element in list is: Reeta

Example 4

If the list(type int) is empty, this function returns zero(0).

In this program, we are creating an empty list(type int). Then, using the std::list::back() function, we are trying to get the last element of the current list(empty).

#include <iostream>
#include <list>
using namespace std;

int main() {
   //create a character list
   list<int> l = {};
   cout<<"The list elements are: ";
   for(int lst : l){
      cout<<lst<<" ";
   }
   cout<<"\nLast element of the list is: "<<l.back()<<endl;
   return 0;
}

Output

The above program generates the following output −

The list elements are: 
Last element of the list is: 0

Example 5

In the following example, we create a list(type char) with the values {'A', 'B','C', 'D'}. Then, we declare an int variable named result that stores the value of list.back() function, and when we print it's value, it displays the ASCII value of the last element of this list.

#include <iostream>
#include <list>
using namespace std;

int main() {
   list<char> char_list = {'A', 'B', 'C', 'D'};
   cout<<"The list elements are: ";
   for(char lst: char_list){
      cout<<lst<<" ";
   }
   //declare an int variable
   int result;
   result = char_list.back();
   cout<<"\nLast element of the list is: "<<char_list.back()<<endl;
   cout<<"The ASCII value of "<<char_list.back()<<" is: "<<result;
}

Output

After executing the above program, it produces the following output −

The list elements are: A B C D 
Last element of the list is: D
The ASCII value of D is: 68
Advertisements