List vs Array

Should I Use a List or an Array?

In C++, start with std::vector, use std::array when the size is fixed, and reach for std::list only in rare cases. Here is a quick way to decide.

On this page
  1. The short answer
  2. Start with the size
  3. Look at how you access the data
  4. Know how vectors grow
  5. Do not pick std::list for speed alone
  6. Other options

The short answer

In C++, start with std::vector. Use std::array when the size is fixed and known at compile time. Use std::list only in the rarer cases described below. Built-in arrays are mostly for older code and C-style interfaces.

The word “list” means different things in different languages. Python’s list and C#’s List<T> are both dynamic arrays, and in C++ that role belongs to std::vector. The C++ std::list is a linked list, which is a different thing.

Start with the size

Ask whether the number of elements is fixed and known at compile time.

If it is, use std::array. A 3D point, for example, always has three coordinates:

#include <array>

std::array<float, 3> position = {0.0f, 1.5f, -2.0f};  // always three values

If it is not, use std::vector. A list of names read from a file, for example, grows as you read:

#include <string>
#include <vector>

std::vector<std::string> names;
names.push_back("Ada");
names.push_back("Grace");

For the basics of the fixed-size option, see “What is std::array in C++?”

Look at how you access the data

  • Reading by index or looping over every item: use std::vector or std::array. Both keep their elements in one block, which is fast to walk.
  • Adding and removing at the end: use std::vector.
  • Inserting and removing in the middle often, while other elements’ iterators must stay valid: use std::list.

If none of these describes your code, std::vector is still usually the better choice.

Know how vectors grow

A std::vector can move its elements to a bigger block when it grows. After that, pointers, references and iterators to its elements can become invalid. If you know roughly how many items you will add, call reserve() first:

std::vector<int> values;
values.reserve(1000);  // one allocation up front
for (int i = 0; i < 1000; ++i) {
    values.push_back(i);
}

If your elements are structs, the same growth rules apply, and “array of structs in c” explains how those elements are laid out in memory.

Do not pick std::list for speed alone

People often reach for std::list because inserts look cheap. An insert is cheap only after you have found the spot, and finding it means walking the nodes one at a time. Each node is a separate allocation, so walking a list is often slower than walking a vector, even with the same number of steps. For small element types, std::vector can win even when you insert in the middle. Measure your own case before you switch.

Other options

std::deque is another option when you need fast adds at both ends. Where the elements of each type live, on the stack or on the heap, is covered in “c heap vs stack.”

The complete guide to list vs array

List vs Array in C++
Read the guide

Keep exploring

Guides from other topics

All topics →

stringstream in C++

stringstream in C++ explained: how the <sstream> classes read and write text in memory, convert strings and numbers, split text, and avoid common mistakes.

stringstream 4 min read