How to Access First Element of a List in C++? Last Updated : 03 Apr, 2024 Comments Improve Suggest changes Like Article Like Report In C++, a list is a sequential container that allows constant time insertions and deletions and allows the users to store data in non-contiguous memory locations. In this article, we will learn how to access the first element in the list in C++. Examples: Input: myList ={1, 2, 3, 4, 5} Output: The first element of the list is 1.Access the First Element of a List in C++To access the first element in a std::list in C++, we can use the std::list::front() method which provides a reference to the first element present in the list. Syntax of std::list::front()list_name.front();C++ Program to Access the First Element of a ListThe following program illustrates how we can access the first element of a list in C++. C++ // C++ Program to illustrate how we can access the first // element of a list #include <iostream> #include <list> using namespace std; int main() { list<int> l1 = { 1, 2, 3, 4, 5 }; // Access the first element using front() int firstElement = l1.front(); // Print the first element cout << "First Element of the list is: " << firstElement << endl; return 0; } OutputFirst Element of the list is: 1 Time Complexity:O(1) Auxiliary Space:O(1) Comment More infoAdvertise with us Next Article How to Access First Element of a List in C++? A abhay94517 Follow Improve Article Tags : C++ Programs C++ STL cpp-list CPP Examples +1 More Practice Tags : CPPSTL Similar Reads How to Access the First Element of a Vector in C++? In this article, we will learn how to access the first element of vector in C++.The most efficient way to access the first element of vector is by using vector front() function. Letâs take a look at a simple example:C++#include <bits/stdc++.h> using namespace std; int main() { vector<int 2 min read How to Access the Last Element of a List in C++? In C++STL, the list container is a doubly-linked list that stores elements in non-contiguous memory locations. In this article, we will learn how to access the last element in a list in C++. For Example, Input: myList = {10, 20, 80, 90, 50}; Output: Last element of the list is : 50Access the Last El 2 min read How to Access the First Element of a Deque in C++? In C++, deques also called double-ended queues are sequence containers that can perform insertions and deletions on both ends. In this article, we will learn how to access the first element of a deque in C++. Example: Input: myDeque = {1,2,3,4,5} Output: Deque Elements: 1 2 3 4 5 First Element: 1Acc 2 min read How to Access Elements of a Pair in C++? In C++, a pair container is defined in <utility> header that can store two values that may be of different data types so as to store two heterogeneous objects as a single unit. In this article, we will learn how to access elements of a pair in C++. Example:Input: myPair={10,G} Output: First El 2 min read How to Access Elements in a Vector in C++? In C++, vector provides fast access to its elements using their position as indexes. In this article, we will discuss different ways to access the element in vector using index in C++.The simplest method to access the elements of vector is by using vector operator [] with the corresponding index val 3 min read Like