std::array in C++


Introduction

In C++11, a new array type called std::array was introduced to provide a safer, more modern alternative to traditional C-style arrays. Unlike regular arrays, std::array comes with various built-in methods for easier manipulation and access to elements, along with benefits like size checking and better integration with standard algorithms.

std::array is part of the C++ Standard Library and is defined in the <array> header. It is a container that encapsulates fixed-size arrays and provides a more convenient, flexible way to handle arrays in modern C++.


Key Features of std::array

  • Fixed Size: Like a C-style array, the size of a std::array is fixed at compile-time and cannot be changed during runtime.
  • Type-Safety: std::array provides type safety, ensuring the elements are of the specified type.
  • Standard Library Support: It integrates seamlessly with STL algorithms and provides several member functions for easy manipulation (e.g., size(), fill(), swap(), etc.).
  • Bound Checking: Unlike traditional arrays, std::array can perform bounds checking when accessing elements using member functions like at().

Syntax

std::array<T, N> array_name;
  • T: The data type of the elements.
  • N: The number of elements in the array, which is known at compile time.

Declaring and Initializing std::array

You can declare a std::array like any other C++ container, but its size is fixed during compile-time.

Example 1: Basic Declaration and Initialization

#include <iostream>
#include <array>

int main() {
    // Declare and initialize std::array
    std::array<int, 5> arr = {1, 2, 3, 4, 5};

    // Access and display elements
    for (int i = 0; i < arr.size(); ++i) {
        std::cout << arr[i] << " ";
    }

    return 0;
}

Explanation:

  • Here, arr is an array of 5 integers.
  • The size() member function returns the number of elements in the array.
  • Elements are accessed using the [] operator.

Example 2: Using at() for Bounds Checking

Unlike traditional arrays, std::array has an at() member function that performs bounds checking.

#include <iostream>
#include <array>

int main() {
    std::array<int, 3> arr = {10, 20, 30};

    // Accessing elements using 'at()' with bounds checking
    for (int i = 0; i < arr.size(); ++i) {
        std::cout << arr.at(i) << " ";
    }

    // This will throw an exception if out-of-bounds access is attempted
    // std::cout << arr.at(5) << std::endl; // Uncommenting this line will throw an exception

    return 0;
}

Explanation:

  • at() provides bounds checking, and it will throw an std::out_of_range exception if an invalid index is accessed.

Important Member Functions of std::array

std::array provides a variety of functions that can make working with arrays easier.

1. size()

Returns the number of elements in the array.

std::array<int, 5> arr = {1, 2, 3, 4, 5};
std::cout << "Size: " << arr.size() << std::endl;  // Output: 5

2. fill()

Fills the entire array with a specified value.

std::array<int, 3> arr = {1, 2, 3};
arr.fill(0);  // Fills the array with 0

for (int i : arr) {
    std::cout << i << " ";  // Output: 0 0 0
}

3. swap()

Swaps the contents of two arrays of the same type and size.

std::array<int, 3> arr1 = {1, 2, 3};
std::array<int, 3> arr2 = {4, 5, 6};

arr1.swap(arr2);  // Swaps arr1 and arr2

for (int i : arr1) {
    std::cout << i << " ";  // Output: 4 5 6
}

4. front() and back()

Accesses the first and last element of the array.

std::array<int, 3> arr = {1, 2, 3};

std::cout << "First element: " << arr.front() << std::endl;  // Output: 1
std::cout << "Last element: " << arr.back() << std::endl;  // Output: 3

5. data()

Returns a pointer to the first element of the array.

std::array<int, 3> arr = {10, 20, 30};
int* ptr = arr.data();

std::cout << "First element via pointer: " << *ptr << std::endl;  // Output: 10

Advantages of Using std::array Over C-style Arrays

  1. Type Safety: With C-style arrays, there’s no guarantee about the type of elements, whereas std::array ensures type safety.
  2. Built-in Functions: std::array provides functions like size(), at(), fill(), etc., that are not available in C-style arrays.
  3. Integration with STL Algorithms: std::array works seamlessly with standard algorithms, such as std::sort, std::find, etc., unlike C-style arrays.
  4. Bound Checking: The at() function offers safe access to elements with bounds checking, preventing out-of-bounds errors.

When to Use std::array

Use std::array when:

  • You know the array size at compile time and need to manipulate the array using modern C++ features.
  • You need safe element access with bounds checking (at()).
  • You want to take advantage of the STL algorithms with array data.
  • You prefer a more modern, type-safe alternative to traditional C-style arrays.