LearnFrenzy
Sign in Take a free test

STL: Vector and Sequence Containers · Lesson 19 of 33

array, deque and list in C++

5 min read Updated Nov 7, 2026 Saurabh Samir

vector is the right default, but the STL has other sequence containers for special cases: std::array when the size is fixed at compile time, std::deque when you need fast insertion at both ends, and std::list when you must insert and remove in the middle without moving anything. Knowing their trade-offs is a common interview question.

In this lesson

  • std::array: fixed size, no heap
  • std::deque: double-ended queue
  • std::list: doubly linked list
  • Choosing a sequence container

std::array

#include <algorithm>
#include <array>
#include <iostream>

int main() {
    std::array<int, 5> marks{78, 91, 64, 85, 70};    // size is part of the type, no heap allocation
    std::sort(marks.begin(), marks.end());
    for (int m : marks) std::cout << m << ' ';
    std::cout << "| size " << marks.size() << ", max " << marks.back() << '\n';

    std::array<int, 3> a{1, 2, 3}, b{1, 2, 3};
    std::cout << std::boolalpha << (a == b) << '\n';          // arrays compare and copy like values
    std::array<std::array<char, 3>, 3> board{{{'X', 'O', 'X'}, {' ', 'X', 'O'}, {'O', ' ', 'X'}}};
    std::cout << "centre: " << board[1][1] << '\n';
    return 0;
}

Output

64 70 78 85 91 | size 5, max 91
true
centre: X

A std::array is a thin wrapper around a C array with no overhead – but it knows its size, can be copied and compared, works with algorithms and never decays to a pointer. Use it instead of raw arrays for fixed sizes (small lookup tables, game boards, coordinates).

std::deque

#include <deque>
#include <iostream>

int main() {
    std::deque<int> d{3, 4, 5};
    d.push_front(2);          // fast at the front (vector can't do this cheaply)
    d.push_front(1);
    d.push_back(6);
    d.pop_front();
    std::cout << "front " << d.front() << ", back " << d.back() << ", d[2] = " << d[2] << '\n';
    for (int x : d) std::cout << x << ' ';
    std::cout << '\n';

    std::deque<int> window;                       // a sliding window of the last 3 readings
    for (int reading : {10, 20, 30, 40, 50}) {
        window.push_back(reading);
        if (window.size() > 3) window.pop_front();
    }
    for (int x : window) std::cout << x << ' ';
    std::cout << '\n';
    return 0;
}

Output

front 2, back 6, d[2] = 4
2 3 4 5 6 
30 40 50 

A deque gives O(1) push_front, push_back, pop_front and pop_back, plus O(1) indexing. Internally it's a set of fixed-size blocks, so elements aren't all contiguous. It's the default container behind std::queue and std::stack, and the tool for 0-1 BFS and sliding-window maximum problems.

std::list

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

int main() {
    std::list<std::string> playlist{"intro", "song A", "song B", "outro"};
    auto it = std::next(playlist.begin(), 2);           // points to "song B"
    playlist.insert(it, "song A.5");                    // O(1): no elements move
    playlist.remove("intro");                           // removes every "intro"
    for (const auto& s : playlist) std::cout << s << " | ";
    std::cout << '\n';
    std::cout << "it still points to: " << *it << '\n';  // list iterators stay valid

    std::list<int> nums{5, 1, 4, 1, 3};
    nums.sort();                                        // member sort (std::sort needs random access)
    nums.unique();                                      // remove consecutive duplicates
    for (int x : nums) std::cout << x << ' ';
    std::cout << '\n';
    return 0;
}

Output

song A | song A.5 | song B | outro | 
it still points to: song B
1 3 4 5 

Lists are slower than you'd think

On paper a list's O(1) middle insertion beats a vector's O(n). In practice, list nodes are scattered in memory, so walking them causes cache misses, and finding the position is O(n) anyway. Vectors usually win even for insert-heavy workloads; use a list only when you need stable iterators or O(1) splicing of whole sub-lists (as in an LRU cache).

Choosing a sequence container

vectorarraydequelist
Sizedynamicfixed at compile timedynamicdynamic
Index [i]O(1)O(1)O(1)no
Add/remove at backO(1)*noO(1)O(1)
Add/remove at frontO(n)noO(1)O(1)
Insert in the middleO(n)noO(n)O(1) given an iterator
Contiguous memoryyesyesin blocksno
Use whendefaultsmall fixed sizequeue at both endsstable iterators, splicing

* amortised

Practice

Try it yourself

  1. Use a deque to check whether a word is a palindrome by comparing and popping both ends.

    Show solution
    #include <deque>
    #include <iostream>
    #include <string>
    
    bool is_palindrome(const std::string& s) {
        std::deque<char> d(s.begin(), s.end());
        while (d.size() > 1) {
            if (d.front() != d.back()) return false;
            d.pop_front();
            d.pop_back();
        }
        return true;
    }
    
    int main() {
        std::cout << std::boolalpha << is_palindrome("level") << ' ' << is_palindrome("vector") << '\n';
        return 0;
    }

    Output

    true false

Quick check

Which container gives O(1) insertion at both ends with indexing?

std::deque.

Why does std::array have no push_back?

Its size is fixed at compile time.

When is std::list a good choice?

When you need iterators that stay valid or O(1) splicing/insertion at known positions.

Summary

  • std::array: fixed size, zero overhead, value semantics.
  • std::deque: fast at both ends with indexing – powers queue and stack.
  • std::list: stable iterators and splicing, but poor cache performance; default to vector.

Finished this lesson?

Mark it complete to track your progress, then carry on with the next lesson.

Next lesson

Search LearnFrenzy

Esc

Quick links