List in C++ STL Last Updated : 04 Oct, 2025 Comments Improve Suggest changes Like Article Like Report A list in C++ is a sequence container that allows you to store elements one after another.Implemented as a doubly linked list and maintains both front and back for fast operations on both the ends.Data is stored in non-contiguous memory, allowing fast insertion and deletion anywhere in the list (beginning, middle, or end). C++ #include <iostream> #include <list> using namespace std; int main() { // Create a list of integers list<int> myList; // Add elements to the list myList.push_back(10); myList.push_back(20); myList.push_front(5); // Display elements cout << "List elements: "; for (int n : myList) { cout << n << " "; } cout << endl; return 0; } OutputList elements: 5 10 20 SyntaxA list is defined as the std::list class template inside the <list> header file.list<T> l;where,T: Type of elements in the list.l: Name assigned to the list.Basic OperationsThe basic operations of list are shown below:1. Inserting Elementsinsert() is used for fast insertion if the position iterator is known; otherwise, traverse the list to reach the position.push_front() is used to insert at the beginning and push_back() to insert at the end.Time complexity to insert element both at beginning and end : O(1). C++ #include <iostream> #include <list> using namespace std; int main() { list<int> l = {3, 2}; // Inserting an element at the end l.push_back(5); // Inserting an element at the beginning l.push_front(1); // Inserting an element at a specific position auto it = l.begin(); advance(it, 2); l.insert(it, 4); for (auto i : l) cout << i << " "; return 0; } Output1 3 4 2 5 2. Accessing ElementsLists do not allow random access, so to get an element at a specific position, you need to go through the list one by one from the start or end.The first and last elements can be accessed quickly using front() and back() methods.Time complexity for accessing elements : O(n) C++ #include <iostream> #include <list> using namespace std; int main() { list<int> l = {1, 3, 4, 2, 5}; // Accessing first and last elements cout << l.front() << endl; cout << l.back() << endl; // Access third element cout << *next(l.begin(), 2); return 0; } Output1 5 43. Updating ElementsList elements can be updated by accessing them with an iterator and using the assignment operator (=) to set a new value.Since lists do not support random access, you must use an iterator to reach the element you want to update.\Time complexity for updating: O(1) if you have the iterator, otherwise O(n) to reach the element. C++ #include <iostream> #include <list> using namespace std; int main() { list<int> l = {1, 3, 4, 2, 5}; // Changing the first element l.front() = 11; // Move iterator to the second element auto it = l.begin(); advance(it, 2); // Update the value using iterator *it = 10; for (auto i : l) cout << i << " "; return 0; } Output11 3 10 2 5 4. Finding ElementsTo find an element in a list, you can use the find() function from the <algorithm> library.find() returns an iterator to the element if it is found, or the end iterator if it is not found.Time complexity for searching elements : O(n). C++ #include <iostream> #include <list> using namespace std; int main() { list<int> l = {1, 3, 4, 2, 5}; // Finding 4 auto it = find(l.begin(), l.end(), 4); if (it != l.end()) cout << *it; else cout << "Element Not Found!"; return 0; } Output45. TraversingA list can be traversed using begin() and end() iterators in a loop.Start from begin() and keep moving the iterator until it reaches end(), accessing each element along the way.Time complexity for traversal : O(n). C++ #include <iostream> #include <list> using namespace std; int main() { list<int> l = {1, 3, 4, 2, 5}; // Traversing using iterators for (auto it = l.begin(); it != l.end(); ++it) cout << *it << " "; return 0; } Output1 3 4 2 5 6. Deleting Elementserase() deletes an element from the list using an iterator to its position.pop_front() and pop_back() quickly delete the first and last elements of the list.Time complexity for deletion : O(1) C++ #include <iostream> #include <list> using namespace std; int main() { list<int> l = {1, 3, 4, 2, 5}; // Deleting last element l.pop_back(); // Deleting first element l.pop_front(); // Deleting third element auto it = l.begin(); advance(it, 2); l.erase(it); for (auto i : l) cout << i << " "; return 0; } Output3 4 Other Member Functions of ListFollowing is the list of all member functions of std::list class in C++:FunctionsDescriptionsize()Returns the number of elements in the list.empty()Checks if the list is empty.rbegin()Returns a reverse iterator pointing to the last element of the list. rend()Returns a reverse iterator pointing to the element before the first element.clear()Removes all elements from the list. List in C++ STL Visit Course Comment More info K kartik Follow Improve Article Tags : C++ STL cpp-containers-library cpp-list Explore C++ BasicsIntroduction to C++3 min readData Types in C++6 min readVariables in C++4 min readOperators in C++9 min readBasic Input / Output in C++4 min readControl flow statements in Programming15+ min readLoops in C++7 min readFunctions in C++8 min readArrays in C++8 min readCore ConceptsPointers and References in C++5 min readnew and delete Operators in C++ For Dynamic Memory5 min readTemplates in C++8 min readStructures, Unions and Enumerations in C++3 min readException Handling in C++12 min readFile Handling through C++ Classes8 min readMultithreading in C++8 min readNamespace in C++5 min readOOP in C++Object Oriented Programming in C++8 min readInheritance in C++6 min readPolymorphism in C++5 min readEncapsulation in C++4 min readAbstraction in C++4 min readStandard Template Library(STL)Standard Template Library (STL) in C++3 min readContainers in C++ STL3 min readIterators in C++ STL10 min readC++ STL Algorithm Library3 min readPractice & ProblemsC++ Interview Questions and Answers1 min readC++ Programming Examples4 min read Like