4 minute read

Introduction

The std::array container in C++ is a fixed-size container that stores elements in a contiguous memory location. It is similar to a traditional C-style array, but it provides additional features and convenience. The std::array container is defined in the <array> header.

Initialization

You can initialize an std::array using the std::array<type, size> name syntax, where type is the data type of the elements, and size is the number of elements in the array. Here is an example of how to initialize an std::array of integers with size 5:

#include <array>

int main() {
    std::array<int, 5> myArray = {1, 2, 3, 4, 5};
    return 0;
}

You can also use the std::array<type, size> name{element1, element2, ...} syntax to initialize the array with elements.

Accessing Elements

You can access the elements of an std::array using the [] operator. Here is an example of how to access the first element of the array:

#include <array>

int main() {
    std::array<int, 5> myArray = {1, 2, 3, 4, 5};
    std::cout << "The first element of the array is: " << myArray[0] << std::endl; //Returns 1
    return 0;
}

You can also use the at() function to access an element of the array, which performs range checking and throws an exception if the index is out of bounds.

std::array<int, 5> myArray = {1, 2, 3, 4, 5};
std::cout << "The first element of the array is: " << myArray.at(0) << std::endl; //Returns 1

Iterating Elements

You can iterate through the elements of an std::array using a range-based for loop or an iterator. Here is an example of how to use a range-based for loop to iterate through the elements of the array:

#include <array>

int main() {
    std::array<int, 5> myArray = {1, 2, 3, 4, 5};
    for (int element : myArray) {
        std::cout << "Element: " << element << std::endl;
    }
    return 0;
}

Removing Elements

As std::array is a fixed-size container, it has a fixed number of elements that are stored in a contiguous memory location. This means that you can’t remove elements from the array directly, as the size of the container can’t be changed at runtime.

Modifying Elements

You can modify the elements of an std::array using the [] operator or the at() function. Here is an example of how to change the value of the first element of the array:

#include <array>

int main() {
    std::array<int, 5> myArray = {1, 2, 3, 4, 5};
    myArray[0] = 10;
    std::cout << "The first element of the array is: " << myArray[0] << std::endl; //Returns 10
    return 0;
}

Deallocating the Array

The std::array container is automatically deallocated when it goes out of scope. However, you can also use the std::array::~array() destructor function to explicitly deallocate the array.

#include <array>

int main() {
    std::array<int, 5> myArray = {1, 2, 3, 4, 5};
    myArray.~array();
    return 0;
}

Keep in mind that, this is unnecessary as the destructor will be called automatically when the array goes out of scope.

Conclusion

It’s worth noting that, as std::array is a fixed-size container, its size is known at compile time and memory allocation is done on the stack, which makes it more efficient than other dynamic container classes like std::vector. However, this also means that the size of an std::array can’t be changed at runtime.

In addition, std::array has the same interface as a traditional C-style array, but it also provides additional features such as begin() and end() functions to access the first and last elements, respectively, and size() function to get the number of elements, which makes it more convenient to use than a traditional C-style array.

In summary, std::array container is a fixed-size container that stores elements in a contiguous memory location, it is similar to a traditional C-style array, but it provides additional features and convenience. It is defined in the <array> header.

References

Read more about the std::array function here: https://www.cplusplus.com/reference/array/