🧩 C++ STL & Data Structures
Estimated reading: 3 minutes 284 views

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::list is and how it works
  • How to declare, traverse, insert, and delete items
  • Differences between list and vector
  • 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

FunctionDescription
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

Featurestd::liststd::vector
Memory layoutNon-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::list is 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 :
Share

C++ Lists

Or Copy Link

CONTENTS
Scroll to Top