List Back Function In C++ STL - C++ Programming Tutorial
C++ Course / Dynamic Programming / List Back Function In C++ STL

List Back Function In C++ STL

BLUF: Mastering List Back Function In C++ STL is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: List Back Function In C++ STL

C++ is renowned for its efficiency. Learn how List Back Function In C++ STL enables low-level control and high-performance computing in the tutorial below.

What is C++ STL?

The acronym STL represents Standard Template Library in the C++ programming language. This library encompasses pre-defined functions and classes designed for a wide range of purposes.

The list represents a data structure that is specified within the standard template library (STL). Numerous built-in functions are employed with the list data structure to add elements at the beginning, end, or at a particular position.

Certain functions are employed to remove elements from a list at the beginning, end, or a designated 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?

Within the list, there exists a back function that provides the reference or pointer to the final element of the list.

If we contrast the end function with the back function, the end function provides the iterator pointing to the final element, while the latter returns the pointer to the last element.

Syntax:

listName.back;

This function does not receive any arguments or parameters.

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 provided code snippet, we define a collection of integer values, followed by adding elements to the collection using the push_back method. As the most recent value added to the collection is ten, querying the last element with the back function reveals the value ten as the final element in the collection.

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 provided code snippet, there is an array containing characters, and the program displays the final element along with its corresponding ASCII code.

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 previous code snippet, we defined a list containing integer values without adding any elements to it. When we call the back function to retrieve the last element, it correctly returns 0 as the final element. Additionally, checking the size of the list also correctly displays 0 as there are no elements present in the list.

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 provided code snippet, a character list was initialized without adding any elements to it. As a result, calling the back function on the empty list will yield an empty or null character value.

If the list happens to be empty and the back function is applied to it, it may exhibit undefined behavior.

Input Required

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

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience