C++ · Module 13 · Lesson 21 min read

Sequence Containers: vector, deque, list

std::vector — the default ✅

Contiguous dynamic array.

  • Fast: random access, push/pop at the back, iteration (cache-friendly)
  • Slow: insert/erase at the front or middle (shifts elements)

std::deque — double-ended queue

Chunks of arrays.

  • Fast: push/pop at both ends, random access
  • Not contiguous; slightly slower iteration than vector
  • References to elements stay valid when pushing at the ends
  • Great for: sliding windows, queues, undo/redo buffers

std::list — doubly linked list

  • Fast: insert/erase anywhere given an iterator (O(1)), splice between lists
  • No random access (l[5] doesn't exist); poor cache performance
  • Iterators/references stay valid until that element is erased
  • Members: push_front, push_back, remove(value), remove_if, sort, unique, reverse, merge, splice

std::forward_list — singly linked list

Even smaller per node; forward-only; insert_after, erase_after.

Complexity cheat sheet

Operationvectordequelist
Access [i]O(1)O(1)❌ O(n)
push/pop backO(1)*O(1)O(1)
push/pop front❌ O(n)O(1)O(1)
insert in middleO(n)O(n)O(1) with iterator
Memorycompactchunks2 pointers per element
*amortized

emplace vs push/insert

emplace_back(args...) constructs the element in place from constructor arguments — avoids a temporary.

Practical advice

Measure before choosing list: because of CPU caches, vector often wins even for middle insertions on small/medium data.

Example

C++
#include <deque>
#include <iostream>
#include <list>
#include <string>
#include <vector>

template <typename C>
void print(const std::string& label, const C& c) {
    std::cout << label << ':';
    for (const auto& x : c) std::cout << ' ' << x;
    std::cout << '\n';
}

int main() {
    // deque as a sliding window of the last 3 daily expenses
    std::deque<int> window;
    for (int spent : { 500, 1200, 300, 800, 950 }) {
        window.push_back(spent);
        if (window.size() > 3) window.pop_front();      // O(1) at the front
    }
    print("last 3 days", window);
    window.push_front(0);
    std::cout << "window[1] = " << window[1] << " (random access works)\n";

    // list: cheap insertion/removal in the middle
    std::list<std::string> playlist = { "Resham", "Kutu Ma Kutu", "Sayad" };
    auto it = playlist.begin();
    ++it;                                              // points to the 2nd song
    playlist.insert(it, "Jhilimili");                   // insert before it, O(1)
    playlist.push_front("Intro");
    print("playlist", playlist);
    playlist.remove("Sayad");                           // remove by value
    playlist.reverse();
    print("reversed", playlist);

    std::list<int> a = { 1, 4, 9 }, b = { 2, 3, 10 };
    a.merge(b);                                         // merge two sorted lists, b becomes empty
    print("merged", a);
    std::cout << "b is now empty? " << std::boolalpha << b.empty() << '\n';

    std::list<int> dupes = { 5, 5, 6, 6, 6, 7, 5 };
    dupes.unique();                                     // removes consecutive duplicates
    print("unique (consecutive)", dupes);

    // vector with emplace_back
    std::vector<std::pair<std::string, int>> scores;
    scores.emplace_back("Asha", 92);                    // constructs the pair in place
    scores.push_back({ "Ravi", 85 });
    for (const auto& [name, score] : scores) std::cout << name << '=' << score << ' ';
    std::cout << '\n';
    return 0;
}

/* Output:
last 3 days: 300 800 950
window[1] = 300 (random access works)
playlist: Intro Resham Jhilimili Kutu Ma Kutu Sayad
reversed: Kutu Ma Kutu Jhilimili Resham Intro
merged: 1 2 3 4 9 10
b is now empty? true
unique (consecutive): 5 6 7 5
Asha=92 Ravi=85
*/