Skip to content
Tutorials Center
My learning

Topics

C++ Reference ยท Operators

The C++ subscript operator []

Reference for the C++ subscript operator []: arrays, vector, map, at() versus operator[], bounds, multidimensional operator[] in C++23 and overloading.

The subscript operator a[i] accesses the element at index i of an array, a pointer, or an object that overloads operator[]. For built-in arrays and pointers, a[i] is defined as *(a + i), which is why pointer arithmetic and indexing are the same thing. Standard containers such as std::vector, std::array, std::string, std::deque and std::map overload it.

operator[] does not check bounds on sequence containers. An out-of-range index is undefined behavior. When you need a checked access, use at(), which throws std::out_of_range.

C++
std::vector<int> v{10, 20, 30};
int x = v[1];     // 20, no bounds check
v.at(5);          // throws std::out_of_range

Try it

C++ Built-in and library subscripts

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

C++ Overloading operator[]

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

Syntax

C++
a[i]                              // built-in or overloaded
a[i, j, k]                        // C++23: overloaded only

T& T::operator[](Index i);              // member only (never a free function)
const T& T::operator[](Index i) const;
decltype(auto) operator[](this Self&& self, Index i);   // C++23 explicit object parameter
Name Description
a An array, a pointer, or an object with operator[].
i The index. For built-in types it must be an integer or unscoped enum; for overloads it can be any type (a string for a map, a pair, a struct).

Returns: a reference to the element for built-in arrays and standard sequence containers, so a[i] = x works. Overloads should also return a reference (const-qualified for the const overload).

Errors / undefined behavior: an index outside [0, size) (or one past the end for pointer arithmetic) is undefined behavior for arrays, pointers, std::vector, std::array, std::string (except s[s.size()], which yields the null character) and std::deque. std::map::operator[] is always defined but may insert.

Usage notes

  • Member only: operator[] must be a non-static member function. Like =, () and ->, it cannot be a free function.
  • Const and non-const overloads: provide both, or a single C++23 version using an explicit object parameter, so that const objects can be read and non-const ones modified.
  • Multi-index (C++23): m[i, j] calls operator[](i, j). Before C++23, m[i, j] was the comma operator and meant m[j]; for older standards use operator()(i, j) or return a row proxy. std::mdspan uses this syntax (not available on the runner).
  • std::map and std::unordered_map: m[key] inserts a value-initialized element when the key is absent, and it is not usable on a const map. Use find, at() or contains() for lookups that must not insert.
  • Negative indices: p[-1] is valid pointer arithmetic when it stays inside the same array. Index types for containers are unsigned, so v[-1] becomes a huge index.
  • Safety: use at(), std::span, a hardened standard library mode (-D_GLIBCXX_ASSERTIONS on libstdc++) or sanitizers in development builds.
  • Proxies: std::vector<bool>::operator[] returns a proxy object instead of bool&, so auto& b = v[0]; does not compile.
  • Performance: v[i] is as fast as raw array access when inlined; at() adds a branch per call.