C++ List and Deque

std::list and std::deque are STL sequence containers that store values in order. They are useful when you need an ordered collection but a vector is not the best fit.

A std::list is designed for fast insertion and removal in the middle of the container. A std::deque, pronounced “deck”, is designed for fast insertion and removal at both the front and the back.

Using a List

To use std::list, include the <list> header. A list does not provide fast index access like items[0]. Instead, you usually loop through it with a range-based for loop or work with iterators.

#include <iostream>
#include <list>
#include <string>

int main(void) {
    std::list<std::string> tasks = {"Plan", "Write", "Test"};

    tasks.push_front("Review");
    tasks.push_back("Publish");
    tasks.remove("Write");

    for (std::string task : tasks) {
        std::cout << task << std::endl;
    }

    return 0;
}

Output:

Review
Plan
Test
Publish

The list starts with three strings. push_front() adds an item to the beginning, push_back() adds an item to the end, and remove() removes every list element equal to the value you pass.

Using a Deque

To use std::deque, include the <deque> header. A deque supports push_front() and push_back(), and it also supports index access with square brackets.

#include <deque>
#include <iostream>

int main(void) {
    std::deque<int> numbers = {20, 30};

    numbers.push_front(10);
    numbers.push_back(40);

    std::cout << "First: " << numbers.front() << std::endl;
    std::cout << "Second: " << numbers[1] << std::endl;
    std::cout << "Last: " << numbers.back() << std::endl;

    numbers.pop_front();
    numbers.pop_back();

    std::cout << "After removing ends:" << std::endl;
    for (int number : numbers) {
        std::cout << number << std::endl;
    }

    return 0;
}

Output:

First: 10
Second: 20
Last: 40
After removing ends:
20
30

This deque grows from both ends. After adding 10 to the front and 40 to the back, the values are 10, 20, 30, and 40. Then pop_front() and pop_back() remove the first and last values.

List vs Deque vs Vector

Container Good at Index access
std::vector Fast access by position and adding at the end Yes
std::list Inserting and removing elements once you have a position No
std::deque Adding and removing at both the front and back Yes

Common Functions

Both std::list and std::deque support functions such as push_back(), pop_back(), front(), back(), size(), and empty(). Both can be used in range-based for loops.

Only some functions apply to one container. For example, std::list has remove(), while std::deque has fast index access with [] and at().

When To Choose Each

Use std::vector as your default ordered container. Choose std::list when you need frequent insertions or removals away from the ends and you do not need indexes. Choose std::deque when you need efficient operations at both the front and back.

Takeaway: std::list is for flexible insertion and removal, while std::deque is for a double-ended sequence with index access.