×

List back () function in C++ STL

The list::back () function of the C++ STL returns a direct reference to the last element in the list container. This function varies from list::end (), which just returns an iterator to the last entry.

Syntax:

list_name. back ()

Parameters

This function doesn't accept any parameters.

Return Value: This function directly references the last element in the list container example list. Although this function does not contain such an exception, applying it on a list container that is empty results in an undefined behaviour in C++.

Errors and Exception

  1. When the list container is empty, it shows unknown behaviour results.
  2. It ensures that no exceptions will be thrown, if the list is not empty.
// C++ program of list back() function 
#include <iostream>
#include <list>
usingnamespacestd;
 
intmain()
{
    list<int>mylist{ 1, 2, 3, 4, 5 };
    cout<<mylist.back();
    return0;
}

Output:

List back () function in C++ STL

The list::back() function is illustrated in the program below.

// assign() method for lists
#include <bits/stdc++.h>
usingnamespacestd;
 
intmain()
{
    // list creation from scratch
    list<int>demo_list;
 
    // Including more items to the list
    demo_list.push_back(10);
    demo_list.push_back(20);
    demo_list.push_back(30);
 
    // prints the last element of demo_list
    cout<<demo_list.back();
 
    return0;
}

Output:

List back () function in C++ STL

Time Complexity: O(1)

Auxiliary Space: O(1)

Application

Print the difference between the first and last entry after adding numbers to an empty list of integers.

Input: 1,2,3,4,5,6,7,8

Output: 7

Explanation: final component =8, initial component=1, difference=7

Algorithm

  1. Using the push front() or push back() function, add numbers to the list.
  2. Compare the first and last components.
  3. Subtract the last element from the first element if it is larger, then print the result.
  4. Otherwise, divide the first element by the last element and output the result.


Code:

// CPP programme to exemplify
// application of the functions front() and back()
#include <iostream>
#include <list>
usingnamespacestd;
 
intmain()
{
    list<int>mylist{};
    mylist.push_front(8);
    mylist.push_front(7);
    mylist.push_front(6);
    mylist.push_front(5);
    mylist.push_front(4);
    mylist.push_front(3);
    mylist.push_front(2);
    mylist.push_front(1);
 
    // listing is now 1, 2, 3, 4, 5, 6, 7, and 8.
 
    if(mylist.front() >mylist.back()) {
        cout<<mylist.front() - mylist.back();
    }
    elseif(mylist.front() <mylist.back()) {
        cout<<mylist.back() - mylist.front();
    }
    else
        cout<< "0";
}

Output:

List back () function in C++ STL

Related Topics

Single dimension array

C++ Array An array is a collection of data (elements) of the same data types. The elements of an array are allocated in contiguous memory allocation. Elements of the array are accessed through...

1 minute read.

C++ Algorithms

There are plenty of programming paradigms that are closely associated with the implementations of code and simulate them into a proper functional one. This is done with the help of...

5 minutes read.

Star pattern in C++ using For Loops

Star patterns are one of the most extensively utilized patterns in any programming language since they help to increase logical thinking and flow control understanding.In the C++ programming language, you...

3 minutes read.

C++ User Defined Exceptions

Overriding and inheriting exception class capabilities may be used to define the new exception. Exception handling can also be used with classes. We may also make an exception for user-defined...

4 minutes read.

Call by Pointer in C++

What is Pointer? Every variable in C++ has a specific address or location in the computer's memory, and this address is known as the memory address. A pointer can be defined...

5 minutes read.

Differences Between C Structures and C++ Structures

The below table clearly describes the differences between C and C++ programming languages Structures concepts where the core principle concepts like static members, data handling, pointers, access modifiers, the importance...

3 minutes read.

C++ | C Plus Plus Data type

Data type in every language is very important. Data type means the different kinds of data that are supported by a particular programming language. A computer language’s data types specify the...

15 minutes read.

C++ File Handling

File handling is a mechanism that manipulates the data stored in files. File handling store output data from the program to external file and read file data to the program. There...

3 minutes read.

OOPs Concepts in C++

C++ Object-Oriented Programming Concepts C++ uses the concept of object-oriented programming. Object Oriented Programming has some prominent features: Object Class Data abstraction Encapsulation Polymorphism Inheritance Message passing Object An object is the basic unit...

2 minutes read.

rand() and srand() in C / C++

In this tutorial, we'll explore the syntax, usage, and examples of the C++ STL functions rand() and srand(). What exactly is rand()? The C++ STL's built-in rand() function is defined in the...

3 minutes read.

Maps in C++

Maps: Maps in C++ are the containers associated with key and mapped values. By keys and mapped values, we mean that the maps are used to store elements formed by the...

4 minutes read.

C++ cin and cout

In this article, we will discuss the C++ cin and cout with their library and examples. C++ Standard Input/Output: User-program communication is made possible by C++’s usage of input and output (I/O)...

5 minutes read.

C++ Comments

Comment/Remark A Comment or a Remark is text that is ignored by the compiler yet is beneficial to programmers. Code is usually annotated with comments for future reference. They are treated...

3 minutes read.

External merge sort in C++

External merge sort in C++ External sorting is a concept for a group of sorting algorithms capable of handling large data volumes. External sorting is needed if the information getting sorted...

9 minutes read.

Function Pointer in C++

Definition: A function pointer in C++ is the same as a usual pointer which is used to point some variables. Function pointers are used to point functions or say store the...

4 minutes read.

Functors in C++

Functors are not a very popular thing among beginner or intermediate-level programmers. But this thing is very useful and helpful. The name functor suggests us some similarities with function. It...

3 minutes read.

Storage Classes in C

Storage Classes in C Storage Classes are used to define the variable and function property. These functionalities include basically the scope, accessibility, and lifetime that help us detect the existence of...

4 minutes read.

C++ add two numbers using the function

Here we will learn how to add two numbers by creating function in C++. Let’s learn this with help of example. Code: - #include <iostream> using namespace std; int add_two_no(int a, int b); int main(){   int...

1 minute read.

Default arguments in C++

Arguments in a function are defined as the values supplied when the function is called. The source is the values supplied, and the destination is the receiving function. Let us...

3 minutes read.

C++ String Concatenation

C++ String Concatenation In this section, we will learn about C ++ String Concatenation, what it does, how it works, and will also see its programs. What is the String Concatenation? The + operator...

3 minutes read.