List vs Array

List vs Array in C++

Compare C++ arrays, std::array, std::vector and std::list by memory layout, element access and insertion cost, with code and a simple rule for choosing.

On this page
  1. Arrays, vectors and lists in C++
  2. How each one stores its elements
  3. Accessing elements
  4. Adding and removing elements
  5. The same data in four containers
  6. Which one to use
  7. Common mistakes

Arrays, vectors and lists in C++

The list vs array question in C++ is really about four types. In C++, “array” usually means a block of values of the same type stored next to each other in memory. “List” usually means std::list, a linked list where each element sits in its own node.

  • Built-in array, such as int a[5];. Its size is fixed at compile time, and it does not know its own length.
  • std::array, from <array>, such as std::array<int, 5>. It is a fixed-size container that knows its size. For the basics, see “What is std::array in C++?”
  • std::vector, from <vector>. A dynamic array that grows as you add elements. It is the closest C++ match for Python’s list and C#’s List<T>.
  • std::list, from <list>. A doubly linked list, where each element has pointers to the element before it and the element after it.

For most code, use std::vector. Use std::array when the size is fixed at compile time. Reach for std::list only when you insert and remove elements in the middle often and need iterators to other elements to stay valid. The reasoning applies to Python’s list and C#’s List<T> too, since both are dynamic arrays underneath.

How each one stores its elements

Built-in arrays, std::array and std::vector keep their elements in one contiguous block of memory. Element 1 sits right after element 0, and so on. This layout lets the CPU read neighboring values together, which is one reason loops over arrays and vectors tend to run fast.

std::list stores each element in its own node, along with pointers to the neighboring nodes. The nodes are allocated separately, so they can be scattered through memory. Those pointers add space for every element, and walking the list means jumping from node to node.

Where the elements live also differs. Elements of a local built-in array or std::array live wherever the object lives, often on the stack. A std::vector always keeps its elements in heap memory, and the vector object holds a pointer to that block.

If the idea of nodes pointing to each other is new, the question “what is a linked list in C” is a good place to start, since std::list is the C++ version of the same idea.

Accessing elements

Built-in arrays, std::array and std::vector support indexing with []. Reaching element i takes the same time whether the container holds ten elements or a million. std::vector and std::array also have at(), which checks the index and throws std::out_of_range if it is invalid. The [] operator does not check the index, and built-in arrays have no checking at all.

std::list has no []. To reach the fourth element, you start at the beginning and step forward, so the time grows with the position of the element.

#include <iostream>
#include <list>
#include <vector>

int main() {
    std::vector<int> v = {10, 20, 30, 40, 50};
    std::cout << v[3] << '\n';     // 40, direct index
    std::cout << v.at(3) << '\n';  // 40, checked index

    std::list<int> l = {10, 20, 30, 40, 50};
    auto it = l.begin();
    for (int i = 0; i < 3; ++i) {
        ++it;                      // step forward one node at a time
    }
    std::cout << *it << '\n';      // 40
}

Adding and removing elements

  • std::vector adds at the end quickly on average with push_back. When it runs out of room, it allocates a larger block and moves its elements, so that one call takes longer. Inserting or erasing in the middle moves every element after that point.
  • std::list inserts or erases in constant time once you have an iterator to the spot, because no other elements move. Finding the spot is the slow part, since you have to walk there. std::list also has push_front, which std::vector does not.
  • Built-in arrays and std::array cannot grow or shrink. Their size is fixed.

Iterator rules differ too. Growing a std::vector can invalidate every iterator, pointer and reference into it. In a std::list, inserting leaves existing iterators valid, and erasing only invalidates iterators to the erased element. If you need to change a vector’s size directly, rather than adding elements one at a time, read up on “How do I resize a vector in C++?”

The same data in four containers

#include <array>
#include <iostream>
#include <list>
#include <vector>

int main() {
    int raw[3] = {1, 2, 3};                // built-in array: fixed size
    std::array<int, 3> fixed = {1, 2, 3};  // std::array: fixed size, knows its size
    std::vector<int> dynamic = {1, 2, 3};  // std::vector: grows as needed
    std::list<int> linked = {1, 2, 3};     // std::list: one node per element

    dynamic.push_back(4);                  // fine: the vector grows
    linked.push_front(0);                  // fine: the list adds at the front
    // fixed.push_back(4);                 // does not compile: std::array cannot grow
    // linked[1];                          // does not compile: std::list has no operator[]

    std::cout << raw[1] << ' ' << fixed[1] << ' ' << dynamic[1] << '\n';                // 2 2 2
    std::cout << fixed.size() << ' ' << dynamic.size() << ' ' << linked.size() << '\n';  // 3 4 4
}

The two commented-out lines show the limits of the fixed and linked types. std::array has no push_back, and std::list has no [].

Which one to use

Use this as a starting rule:

  • std::vector is the default. It is fast to read by index, fast to add to at the end, and it works well with the rest of the standard library.
  • std::array fits when the number of elements is fixed and known at compile time, such as three coordinates or a fixed-size board.
  • Built-in arrays are mostly for older code and C-style interfaces.
  • std::list fits a narrower case: you insert and remove in the middle often, and you need iterators or references to other elements to stay valid while you do it.
  • std::deque is another option when you need fast adds at both ends.

Measure before you switch to std::list for speed. Insertions in the middle look cheap on paper, but finding the position costs time, and std::vector can beat std::list even for middle inserts when the elements are small.

Common mistakes

  • Expecting [] on a list. std::list has no []. Use iterators, or switch to std::vector if you need indexing.
  • Returning a built-in array from a function. C++ functions cannot return one by value. Use std::array or std::vector instead. “how to return an array in c” covers the other options.
  • Keeping a pointer into a vector while adding to it. If the vector reallocates, the pointer is left dangling. Call reserve() when you know the size, or store an index instead of a pointer.

Keep exploring

Guides from other topics

All topics →