List Back Function In C++ STL

What is C++ STL?

STL stands for Standard Template Library in C++. This library contains inbuilt functions and classes for various uses.

The list is also the data structure which is defined in the standard template library (STL). There are a lot of in-built functions used with the list data structure to insert the elements at starting, ending or at any specific position.

Some functions are used to delete the elements from the list at the starting, ending or at any specific position.

For example:

  • insert function is used to put the elements at any particular position.
  • push_back function is used to put the elements from the ending position.
  • push_front function is used to put the elements from the front of the list.
  • pop_back function is used to delete the elements from the ending position.
  • pop_front function is used to delete the elements from the starting position.
  • size function is used to get the size of the list or the number of elements present in the list.
  • front function is used to get the front element of the list.
  • back function is used to get the element which is present at the end of the list.
  • swap function is used to swap the two lists of the same size.
  • reverse function is used to reverse a list completely.
  • sort function is used to sort the list. (by default, ascending order)
  • splice function is used to transfer the elements of one list into another list.
  • merge function is used to merge the two lists.
  • What is back function?

In the list, we have a back function which returns the reference or pointer to the last element of the list.

If we compare the end function to back function, the end function returns the iterator to the last element, whereas this returns the pointer to the last element.

Syntax:

listName.back;

This function does not accept any argument or parameter.

C++ Example 1:

Example

#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int main() {
    //declaring the list
    list<int> myList;
    myList.push_back(1);
    myList.push_back(2);
    myList.push_back(3);
    myList.push_back(4);
    myList.push_back(5);
    
    myList.push_back(6);
    myList.push_back(7);
    myList.push_back(8);
    myList.push_back(9);
    myList.push_back(10);
    
    int lastElement = myList.back();
    cout<<"last element of the list is : "<<lastElement<<endl;
    return 0;
}
]

Output:

Explanation

In the above code, we declare a list of integers, and then we push the elements in the list using the push_back function. Since the last element of the list we pushed was ten, and when we use the back function and print the element, it prints ten as the last element.

C++ Example 2:

Example

#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int main() {
    
    list<char> myList;
    myList.push_back('a');
    myList.push_back('b');
    myList.push_back('c');
    myList.push_back('d');
    myList.push_back('e');
    myList.push_back('f');
    myList.push_back('g');
    myList.push_back('h');
    myList.push_back('i');
    myList.push_back('j');
    int lastElement_int = myList.back();
    char lastElement_ch = myList.back();
    int listSize = myList.size();
    cout<<"last element of the list is : "<<lastElement_ch<<endl;
    cout<<"last element of the list in ascii value is : "<<lastElement_int<<endl;
    cout<<"the size of the list is : "<<listSize<<endl;
    return 0;
}

Output:

Explanation

In the above code, we have a list of characters, and we have printed the last element and its ASCII value.

C++ Example 3:

When the list is empty:

Example

#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int main() {
    //declaring the list
    list<int> myList;
    int lastElement = myList.back();
    int listSize = myList.size();
    cout<<"last element of the list is : "<<lastElement<<endl;
    cout<<"the size of the list is : "<<listSize<<endl;
    return 0;
}

Output:

Explanation

In the above code, we declared the list of type integers, but we did not push any elements in the list. Now, if we use the back function to get the last element, it returns 0 as the last element, and if we print the size of the list, then it returns 0, which is correct.

C++ Example 4:

Example

#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int main() {
    //declaring the list of characters
    list<char> myList;
    char lastElement = myList.back();
    int listSize = myList.size();
    cout<<"last element of the list is : "<<lastElement<<endl;
    cout<<"the size of the list is : "<<listSize<<endl;
    return 0;
}

Output:

Explanation

In the above code, we have created the list of characters but did not push any element in the list. Since the list is empty and when we use the back function to get the last element of the list, it returns an empty or null character.

So, if the list is empty and if we use the back function for the list, it shows some undefined behavior.

Input Required

This code uses input(). Please provide values below: