list::front() and list::back() in C++ STL
Last Updated :
23 Jun, 2022
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
- If the list container is empty, it causes undefined behavior
- It has a no exception throw guarantee if the list is not empty
C++
#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
- If the list container is empty, it causes undefined behavior
- It has a no exception throw guarantee if the list is not empty
C++
#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++
#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);
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
list::pop_front() and list::pop_back() in C++ STL
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::pop_front() pop_front() function is used to
4 min read
list::push_front() and list::push_back() in C++ STL
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::push_front() push_front() function is used
4 min read
list::emplace_front() and list::emplace_back() in C++ STL
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::emplace_front()This function is used to ins
3 min read
list::begin() and list::end() in C++ STL
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::begin() begin() function is used to return
3 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
1 min read
vector::front() and vector::back() in C++ STL
In C++, the vector front() is a built-in function used to retrieve the first element of the vector. It provides a reference to the first element, allowing you to read or modify it directly. Letâs take a quick look at a simple example that uses the vector front() method: [GFGTABS] C++ #include <bi
2 min read
list push_back() function in C++ STL
The list:push_back() function in C++ STL is used to add a new element to an existing list container. It takes the element to be added as a parameter and adds it to the list container. Syntaxlist_name.push_back(value)ParametersThis function accepts a single parameter which is a mandatory value. This
1 min read
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
3 min read
list::empty() and list::size() in C++ STL
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::empty()empty() function is used to check if
3 min read
list pop_back() function in C++ STL
The list::pop_back() is a built-in function in C++ STL which is used to remove an element from the back of a list container. That is, this function deletes the last element of a list container. This function thus decreases the size of the container by 1 as it deletes an element from the end of the l
2 min read