C++ Lists β Master Doubly Linked Lists with std::list
Introduction β Why Use std::list in C++
In situations where you need frequent insertions or deletions anywhere in a collection, std::list is a better choice than std::vector. Unlike vectors, which store elements in contiguous memory, a C++ list is a doubly linked list, enabling constant time insert/remove operations at any position.
In this guide, youβll learn:
- What
std::listis and how it works - How to declare, traverse, insert, and delete items
- Differences between
listandvector - Best practices and real-world use cases
What Is a C++ List?
A std::list is a container that implements a doubly linked list, meaning each node has pointers to both the previous and next elements.
Include it with:
#include <list>
Declare a list:
list<int> numbers;
Code Examples β With Output
Declaring and Adding Elements
#include <iostream>
#include <list>
using namespace std;
int main() {
list<int> l;
l.push_back(10);
l.push_front(5);
l.push_back(15);
for (int x : l) cout << x << " ";
return 0;
}
Output:
5 10 15
Inserting and Erasing
list<int>::iterator it = l.begin();
advance(it, 1);
l.insert(it, 8); // Insert before position 1
l.erase(it); // Remove element at position 1
List Operations
| Function | Description |
|---|---|
push_back() | Adds element at the end |
push_front() | Adds element at the beginning |
pop_back() | Removes element from the end |
pop_front() | Removes element from the front |
insert() | Inserts at specific position |
erase() | Removes specific position or range |
remove(val) | Removes all occurrences of value |
sort() | Sorts list elements |
reverse() | Reverses list order |
unique() | Removes consecutive duplicates |
Iterating Through a List
for (list<int>::iterator it = l.begin(); it != l.end(); ++it)
cout << *it << " ";
Or simply:
for (int x : l)
cout << x << " ";
List vs Vector β Key Differences
| Feature | std::list | std::vector |
|---|---|---|
| Memory layout | Non-contiguous (linked list) | Contiguous (array) |
| Random access | No | Yes (v[i]) |
| Insertion/Removal | Fast anywhere | Slow in the middle |
| Iterator stability | Stable on insertion/removal | May invalidate on reallocation |
| Overhead | Higher (due to pointers) | Lower |
Best Practices & Tips
Use std::list for applications where insertions/removals in the middle are frequent
Donβt use std::list for index-based accessβit lacks random access
Avoid sorting large lists manuallyβuse .sort() member function
Combine unique() and sort() to remove all duplicates
Use Cases for Lists
Undo/Redo Buffers β Insert/remove operations at any position
Editor History β Constant time navigation
Message Queues β Fast push/pop from both ends
Simulation Queues β Realtime event queues with precise ordering
Playlist/Media Queues β Easy reshuffling and movement
Summary β Recap & Next Steps
Key Takeaways:
std::listis a doubly linked list that supports fast insertion/removal at both ends and middle- Ideal for operations requiring frequent reordering or splicing
- Not suitable for random access tasks
Real-World Relevance:
Used in schedulers, compilers, GUIs, game state history, and real-time processing engines where flexible element movement is critical.
Next Steps:
Learn about C++ Stacks, built on deque or vector, to implement LIFO structures easily.
FAQ β C++ Lists
How is list different from vector?list uses non-contiguous memory and allows fast middle insertions; vector uses contiguous memory and supports fast random access.
Can I sort a list with std::sort()?
No. Use list.sort() instead. std::sort() requires random-access iterators.
Does list allow duplicates?
Yes. Use unique() to remove consecutive duplicates.
Can I reverse a list?
Yes. Use .reverse().
How do I remove all instances of a value?
Use list.remove(value);.
Share Now :
