std::array


Container: std::array

std::array is a container in the C++ Standard Library that represents a fixed-size array. Unlike traditional C-style arrays, std::array provides a more convenient and safer interface with additional functionalities. std::array is part of the sequence containers, along with std::vector, std::deque, and others, but it differs in that its size is fixed at compile time and cannot be changed.

Key Characteristics of std::array

Example 1: Basic Usage of std::array

Let's explore some examples to understand how std::array works.

#include <iostream>
#include <array>

int main() {
    // Create a std::array of 5 integers
    std::array<int, 5> myArray = {1, 2, 3, 4, 5};

    // Access elements using the index operator
    std::cout << "First element: " << myArray[0] << std::endl;
    std::cout << "Second element: " << myArray[1] << std::endl;

    // Iterate over the array using a range-based for loop
    std::cout << "Elements: ";
    for (int value : myArray) {
        std::cout << value << " ";
    }
    std::cout << std::endl;

    return 0;
}

Explanation:

Example 2: Using std::array with Standard Algorithms

std::array integrates seamlessly with standard algorithms provided by the C++ Standard Library.

#include <iostream>
#include <array>
#include <algorithm>

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

    // Sort the array
    std::sort(myArray.begin(), myArray.end());

    // Find an element
    auto it = std::find(myArray.begin(), myArray.end(), 3);

    // Print sorted array
    std::cout << "Sorted elements: ";
    for (int value : myArray) {
        std::cout << value << " ";
    }
    std::cout << std::endl;

    // Print the result of the find operation
    if (it != myArray.end()) {
        std::cout << "Found element: " << *it << std::endl;
    } else {
        std::cout << "Element not found" << std::endl;
    }

    return 0;
}

Explanation:

Example 3: Accessing Array Properties

std::array provides several useful member functions to access its properties, such as size(), empty(), front(), back(), and data().

#include <iostream>
#include <array>

int main() {
    std::array<int, 5> myArray = {10, 20, 30, 40, 50};

    std::cout << "Array size: " << myArray.size() << std::endl;
    std::cout << "First element: " << myArray.front() << std::endl;
    std::cout << "Last element: " << myArray.back() << std::endl;

    int* dataPtr = myArray.data();
    std::cout << "Data pointer: " << dataPtr << ", First element via pointer: " << *dataPtr << std::endl;

    return 0;
}

Explanation:

Example 4: Multidimensional std::array

std::array can be used to create multidimensional arrays by nesting std::array types.

#include <iostream>
#include <array>

int main() {
    // Create a 2D array (3x3 matrix)
    std::array<std::array<int, 3>, 3> matrix = {{
        {1, 2, 3},
        {4, 5, 6},
        {7, 8, 9}
    }};

    // Access and print the elements of the 2D array
    for (const auto& row : matrix) {
        for (int value : row) {
            std::cout << value << " ";
        }
        std::cout << std::endl;
    }

    return 0;
}

Explanation:

Example 5: Bounds Checking with at()

The at() method in std::array provides bounds-checked access to elements, throwing an exception if the index is out of bounds.

#include <iostream>
#include <array>

int main() {
    std::array<int, 5> myArray = {10, 20, 30, 40, 50};

    try {
        std::cout << "Element at index 2: " << myArray.at(2) << std::endl;
        std::cout << "Element at index 5: " << myArray.at(5) << std::endl;  // This will throw an exception
    } catch (const std::out_of_range& e) {
        std::cerr << "Exception: " << e.what() << std::endl;
    }

    return 0;
}

Explanation:

Comparison with C-Style Arrays

Summary

std::array is an essential tool in C++ programming when you need a fixed-size array with the benefits of type safety, a standard interface, and better integration with modern C++ features. It provides the efficiency of C-style arrays while avoiding many of the pitfalls associated with raw arrays.

Previous Page | Course Schedule | Course Content