cyberangles blog

`array::operator[]` in C++ STL

In the world of C++, the Standard Template Library (STL) provides a wide range of containers and algorithms to simplify and optimize the development process. One such container is std::array, which is a fixed - size array container introduced in C++11. The array::operator[] is a crucial member function that allows you to access the elements of an std::array using the familiar square - bracket notation. This blog post will provide an in - depth look at array::operator[], including its functionality, common practices, best practices, and example usage.

2026-07

Table of Contents#

  1. What is std::array?
  2. Understanding array::operator[]
  3. Common Practices
  4. Best Practices
  5. Example Usage
  6. Conclusion
  7. References

What is std::array?#

std::array is a container in the C++ STL that represents a fixed - size array. Unlike the traditional C - style arrays, std::array is a class template that provides a more object - oriented and safer way to work with arrays. It has a fixed size determined at compile - time, and all elements are stored in contiguous memory locations.

The syntax to declare an std::array is as follows:

#include <array>
// Syntax: std::array<type, size> array_name;
std::array<int, 5> myArray;

Here, int is the type of elements stored in the array, and 5 is the size of the array.

Understanding array::operator[]#

The array::operator[] is a member function of the std::array class that allows you to access the elements of the array by their index. It takes an integer index as an argument and returns a reference to the element at that index.

The syntax of array::operator[] is:

reference operator[]( size_type pos );
const_reference operator[]( size_type pos ) const;
  • The non - const version returns a reference to the element at position pos, which allows you to modify the element.
  • The const version is used when the std::array object is declared as const. It returns a const reference to the element at position pos, preventing any modifications to the element.

Common Practices#

Basic Element Access#

The most common use of array::operator[] is to access and modify individual elements of an std::array.

#include <iostream>
#include <array>
 
int main() {
    std::array<int, 3> arr = {1, 2, 3};
    // Access an element
    std::cout << "Element at index 1: " << arr[1] << std::endl;
    // Modify an element
    arr[2] = 4;
    std::cout << "Modified element at index 2: " << arr[2] << std::endl;
    return 0;
}

In this example, we first access the element at index 1 and then modify the element at index 2.

Iterating Over an Array#

You can use array::operator[] to iterate over all the elements of an std::array.

#include <iostream>
#include <array>
 
int main() {
    std::array<int, 4> arr = {10, 20, 30, 40};
    for (size_t i = 0; i < arr.size(); ++i) {
        std::cout << "Element at index " << i << ": " << arr[i] << std::endl;
    }
    return 0;
}

This code iterates over the std::array using a for loop and accesses each element using array::operator[].

Best Practices#

Boundary Checking#

One of the limitations of array::operator[] is that it does not perform boundary checking. If you try to access an index outside the valid range of the array, it will lead to undefined behavior. To avoid this, you should always ensure that the index is within the valid range.

#include <iostream>
#include <array>
 
int main() {
    std::array<int, 3> arr = {1, 2, 3};
    size_t index = 2;
    if (index < arr.size()) {
        std::cout << "Element at index " << index << ": " << arr[index] << std::endl;
    } else {
        std::cout << "Index out of range." << std::endl;
    }
    return 0;
}

In this example, we check if the index is within the valid range before accessing the element.

Using at() for Safer Access#

If you need a safer alternative to array::operator[], you can use the at() member function. The at() function performs boundary checking and throws a std::out_of_range exception if the index is out of range.

#include <iostream>
#include <array>
 
int main() {
    std::array<int, 3> arr = {1, 2, 3};
    try {
        std::cout << "Element at index 1: " << arr.at(1) << std::endl;
        std::cout << "Element at index 5: " << arr.at(5) << std::endl;
    } catch (const std::out_of_range& e) {
        std::cout << "Exception: " << e.what() << std::endl;
    }
    return 0;
}

In this example, the first call to at() is successful, but the second call throws an exception because the index is out of range.

Example Usage#

Matrix Representation#

You can use std::array and array::operator[] to represent a matrix.

#include <iostream>
#include <array>
 
int main() {
    std::array<std::array<int, 3>, 2> matrix = {{{1, 2, 3}, {4, 5, 6}}};
    for (size_t i = 0; i < matrix.size(); ++i) { // Iterate over rows
        for (size_t j = 0; j < matrix[i].size(); ++j) { // Iterate over columns
            std::cout << matrix[i][j] << " ";
        }
        std::cout << std::endl;
    }
    return 0;
}

In this example, we create a 2D matrix using std::array and access its elements using array::operator[].

Conclusion#

The array::operator[] in C++ STL is a powerful and convenient way to access the elements of an std::array. It provides a familiar syntax similar to traditional C - style arrays. However, it is important to be aware of its limitations, such as the lack of boundary checking. By following the best practices, you can use array::operator[] safely and effectively in your C++ programs.

References#