The Wayback Machine - https://web.archive.org/web/20240926213544/https://www.geeksforgeeks.org/list-front-function-in-c-stl/
Open In App

list front() function in C++ STL

Last Updated : 29 May, 2023
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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.

Syntax

list_name.front();

Parameters

  • This function does not accept any parameter, it simply returns a reference to the first element in the list container.

Return Value

  • This function returns a direct reference to the first element in the list container.

Exception

  • This function creates an undefined behavior when used with an empty list container.

Example

The below program illustrates the list::front() function.

C++




// CPP program to illustrate the
// list::front() function
#include <bits/stdc++.h>
using namespace std;
 
int main()
{
    // Creating a list
    list<int> demoList;
 
    // Add elements to the List
    demoList.push_back(10);
    demoList.push_back(20);
    demoList.push_back(30);
    demoList.push_back(40);
 
    // get the first element using front()
    int ele = demoList.front();
 
    // Print the first element
    cout << ele;
 
    return 0;
}


Output

10

Time complexity: O(1)
Auxiliary Space: O(n) where n is the size of the list

Note: This function works in constant time complexity.


Previous Article
Next Article

Similar Reads

list::front() and list::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::front() This function is used to reference the first element of the list container. This func
3 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 be used to fetch the first element of a queue.Syn
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
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
vector::front() and vector::back() in C++ STL
The vector::front() and vector::back() function in C++ STL are used to fetch the first and the last element of the std::vector container respectively. They are the member function of std::vector class and defined inside <vector> header file. In this article, we will learn about the vector::front() and vector::back() and how they are different
2 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 to push elements into a list from the front. The n
4 min read
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 pop or remove elements from a list from the front
4 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 the list container is empty or not. Syntax : list
3 min read
list::remove() and list::remove_if() 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::remove() remove() function is used to remove all the values from the list that correspond to
3 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 insert a new element into the list container, the new
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 an iterator pointing to the first element of the l
3 min read
list cbegin() and cend() function in C++ STL
The list::cbegin() is a built-in function in C++ STL which returns a constant random access iterator which points to the beginning of the list. Hence the iterator obtained can be used to iterate container but cannot be used to modify the content of the object to which it is pointing even if the object itself is not constant.Syntax: list_name.cbegin
2 min read
list assign() function in C++ STL
The list::assign() is a built-in function in C++ STL which is used to assign values to a list. It can also be used to copy elements from one list to another. To assign elements to a list. Syntax: list_name.assign(count, value) Parameters: This function accepts two mandatory parameters as shown in the above syntax and described below: count: The num
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 refers to the elements needed to be added to the l
1 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 remove() function in C++ STL
The list::remove() is a built-in function in C++ STL which is used to remove elements from a list container. It removes elements comparing to a value. It takes a value as the parameter and removes all the elements from the list container whose value is equal to the value passed in the parameter of the function. Syntax: list_name.remove(val) Paramet
2 min read
list erase() function in C++ STL
The list::erase() is a built-in function in C++ STL which is used to delete elements from a list container. This function can be used to remove a single element or a range of elements from the specified list container. Syntax: iterator list_name.erase(iterator position)or,iterator list_name.erase(iterator first, iterator last)Parameters: This funct
3 min read
list end() function in C++ STL
The list::end() is a built-in function in C++ STL which is used to get an iterator to past the last element. By past the last element it is meant that the iterator returned by the end() function return an iterator to an element which follows the last element in the list container. It can not be used to modify the element or the list container. This
2 min read
list empty() function in C++ STL
The list::empty() is a built-in function in C++ STL that is used to check whether a particular list container is empty or not. This function does not modify the list, it simply checks whether a list is empty or not, i.e. the size of the list is zero or not. Syntaxlist_name.empty() Parameters This function does not accept any parameter, it simply ch
1 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 list. Syntaxlist_name.pop_back();ParametersThe func
2 min read
list pop_front() function in C++ STL
The list::pop_front() is a built-in function in C++ STL which is used to remove an element from the front of a list container. This function thus decreases the size of the container by 1 as it deletes the element from the front of the list. Syntaxlist_name.pop_front();ParametersThe function does not accept any parameter.Return ValueThis function do
1 min read
list reverse function in C++ STL
The list::reverse() is a built-in function in C++ STL which is used to reverse a list container. It reverses the order of elements in the list container. Syntax: list_name.reverse()Parameters: This function does not accept any parameters. Return Value: This function does not return any value. It just reverses the order of elements in the list conta
1 min read
list resize() function in C++ STL
The list::resize() is a built-in function in C++ STL which is used to resize a list container. It takes a number n as parameter and resizes the list container to contain exactly n elements. If the list already has more than n elements, then the function erases the elements from the list except the first n element.If the list contains less than n el
2 min read
list size() function in C++ STL
The list::size() is a built-in function in C++ STL that is used to find the number of elements present in a list container. That is, it is used to find the size of the list container.Syntax: list_name.size(); Time Complexity - Linear O(1) as per c++11 standard. Possible because the implementation maintains the size field. Parameters: This function
1 min read
list max_size() function in C++ STL
The list::max_size() is a built-in function in C++ STL which returns the maximum number of elements a list container can hold. Syntax: list_name.max_size()Parameters: This function does not accept any parameters. Return Value: This function returns the maximum number of elements a list container can hold. Below program illustrate the list::max_size
1 min read
list rbegin() and rend() function in C++ STL
list::rbegin() is an inbuilt function in C++ STL that returns a reverse iterator which points to the last element of the list. Syntax: list_name.rbegin()Parameter: This function does not accept any parameters. Return value: It returns a reverse iterator which points to the last element of the list. Below program illustrates the above function: C/C+
2 min read
list emplace() function in C++ STL
The list::emplace() is a built-in function in C++ STL which extends list by inserting new element at a given position. Syntax: list_name.emplace(position, element)Parameters: The function accepts two mandatory parameters which are described below: position - it specifies the iterator which points to the position in the list where the new element is
2 min read
list merge() function in C++ STL
The list::merge() is an inbuilt function in C++ STL which merges two sorted lists into one. The lists should be sorted in ascending order. If no comparator is passed in parameter, then it merges two sorted lists into a single sorted list. If a comparator is passed in the parameter, then it merges the list accordingly doing internal comparisons. Syn
2 min read
list crbegin() and crend() function in C++ STL
The list::crbegin() is a built-in function in c++ STL that returns a constant reverse iterator which points to the last element of the list i.e reversed beginning of container. The elements cannot be modified or changed as the iterator is a constant one. Syntax: list_name.crbegin()Parameters: The function does not accept any parameters. Return valu
3 min read
Article Tags :
Practice Tags :