The Wayback Machine - https://web.archive.org/web/20240724091425/https://www.geeksforgeeks.org/listfront-and-listback-in-cpp-stl/
Open In App

list::front() and list::back() in C++ STL

Last Updated : 23 Jun, 2022
Improve
Suggest changes
Post a comment
Like Article
Like
Save
Share
Report

Lists are containers used in C++ to store data in a non-contiguous fashion, Normally, Arrays and Vectors are contiguous in nature, therefore the insertion and deletion operations are costlier as compared to the insertion and deletion option in Lists.
 

list::front()

This function is used to reference the first element of the list container. This function can be used to fetch the first element of a list.

Syntax :  

listname.front()
Parameters :
No value is needed to pass as the parameter.
Returns :
Direct reference to the first element of the list container.

Examples: 

Input  : list list{1, 2, 3, 4, 5};
         list.front();
Output : 1

Input  : list list{0, 1, 2, 3, 4, 5};
         list.front();
Output : 0

Errors and Exceptions 

  1. If the list container is empty, it causes undefined behavior
  2. It has a no exception throw guarantee if the list is not empty

C++




// CPP program to illustrate
// Implementation of front() function
#include <iostream>
#include <list>
using namespace std;
 
int main()
{
    list<int> mylist{ 1, 2, 3, 4, 5 };
    cout << mylist.front();
    return 0;
}


Output: 

1
list::back()

This function is used to reference the last element of the list container. This function can be used to fetch the first element from the end of a list.

Syntax : 

listname.back()
Parameters :
No value is needed to pass as the parameter.
Returns :
Direct reference to the last element of the list container.

Examples:  

Input  : list list{1, 2, 3, 4, 5};
         list.back();
Output : 5

Input  : list list{1, 2, 3, 4, 5, 6};
         list.back();
Output : 6

Errors and Exceptions  

  1. If the list container is empty, it causes undefined behavior
  2. It has a no exception throw guarantee if the list is not empty

C++




// CPP program to illustrate
// Implementation of back() function
#include <iostream>
#include <list>
using namespace std;
 
int main()
{
    list<int> mylist{ 1, 2, 3, 4, 5 };
    cout << mylist.back();
    return 0;
}


Output: 

5

Application 
Given an empty list of integers, add numbers to the list, then print the difference between the first and the last element.

Input: 1, 2, 3, 4, 5, 6, 7, 8
Output:7
Explanation: Last element = 8, First element = 1, Difference = 7

Algorithm 
1. Add numbers to the list using push_front() or push_back() function 
2. Compare the first and the last element. 
3. If first element is larger, subtract last element from it and print it. 
4. Else subtract first element from the last element and print it.

C++




// CPP program to illustrate
// application Of front() and back() function
#include <iostream>
#include <list>
using namespace std;
 
int main()
{
    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);
 
    // list becomes 1, 2, 3, 4, 5, 6, 7, 8
 
    if (mylist.front() > mylist.back()) {
        cout << mylist.front() - mylist.back();
    }
    else if (mylist.front() < mylist.back()) {
        cout << mylist.back() - mylist.front();
    }
    else
        cout << "0";
}


Output: 

7

Let us see the differences in a tabular form -:

  list::front() list::back()
1. It is used to return a reference to the first element in the list container It is used to return a reference to the last element in the list container.
2. Its syntax is -:
 reference front();

Its syntax is -:

 reference back();

3. It does not take any parameters. It does not take any parameters.
4. Its complexity is constant. Its complexity is constant.
5. Its iterator validity does not changes. Its iterator validity does not changes.

 



Similar Reads

queue::front() and queue::back() in C++ STL
Queue are a type of container adaptors which operate in a first in first out (FIFO) type of arrangement. Elements are inserted at the back (end) and are deleted from the front. queue::front() This function is used to reference the first or the oldest element of the queue container. This function can be used to fetch the first element of a queue.Syn
3 min read
vector::front() and vector::back() in C++ STL
Vectors are same as dynamic arrays with the ability to resize itself automatically when an element is inserted or deleted, with their storage being handled automatically by the container. vector::front() This function can be used to fetch the first element of a vector container.Syntax : vectorname.front() Parameters : No value is needed to pass as
3 min read
array::front() and array::back() in C++ STL
Array classes are generally more efficient, light-weight, and reliable than C-style arrays. The introduction of array class from C++11 has offered a better alternative for C-style arrays. array::front() This function is used to reference the first element of the array container. This function can be used to fetch the first element of an array. Synt
3 min read
Deque::front() and deque::back() in C++ STL
Deque or Double Ended queues are sequence containers with the feature of expansion and contraction on both ends. They are similar to vectors, but are more efficient in case of insertion and deletion of elements at the end, and also at the beginning. Unlike vectors, contiguous storage allocation may not be guaranteed in the deque. deque::front()fron
4 min read
list back() function in C++ STL
The list::back() function in C++ STL returns a direct reference to the last element in the list container. This function is different from the list::end() function as the end() function returns only the iterator to the last element. Syntaxlist_name.back();ParametersThis function does not accept any parameter.Return ValueReturns a direct reference t
1 min read
list front() function in C++ STL
The list::front() is a built-in function in C++ STL which is used to return a reference to the first element in a list container. Unlike the list::begin() function, this function returns a direct reference to the first element in the list container. Syntaxlist_name.front(); Parameters This function does not accept any parameter, it simply returns a
1 min read
forward_list::front() and forward_list::empty() in C++ STL
Forward list in STL implements singly linked list. Introduced from C++11, forward list are useful than other containers in insertion, removal and moving operations (like sort) and allows time constant insertion and removal of elements.It differs from list by the fact that forward list keeps track of location of only next element while list keeps tr
3 min read
Sum of shortest distance on source to destination and back having at least a common vertex
Given a directed weighted graph and the source and destination vertex. The task is to find the sum of shortest distance on the path going from source to destination and then from destination to source such that both the paths have at least a common vertex other than the source and the destination. Note: On going from destination to source, all the
15+ min read
Difference Between deque::assign and deque::back in C++
Deque or Double-ended queues are sequence containers with the feature of expansion and contraction on both ends. They are similar to vectors, but are more efficient in the case of insertion and deletion of elements at the end, and also the beginning. Unlike vectors, contiguous storage allocation may not be guaranteed. Deque::assign deque::assign is
3 min read
std::string::back() in C++ with Examples
This function returns a direct reference to the last character of the string. This shall only be used on non-empty strings. This can be used to access the last character of the string as well as to append a character at the end of the string. Length of the string remains unchanged after appending a character, last character of string gets replaced
2 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg