array


Include file: <array>

The <array> header, introduced in C++11, provides the std::array container, which is a fixed-size array that offers the benefits of C-style arrays with additional features of STL containers. It encapsulates a fixed-size array as a template class, providing better type safety and convenience compared to C-style arrays.

Key Characteristics

Key Components

  1. std::array<T, N>: The main container class, where T is the element type and N is the fixed size

Example 1: Basic Usage

#include <iostream>
#include <array>

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

    std::cout << "Size: " << numbers.size() << std::endl;
    std::cout << "Element at index 2: " << numbers[2] << std::endl;

    for (int num : numbers) {
        std::cout << num << " ";
    }
    std::cout << std::endl;

    return 0;
}

Explanation

Example 2: Using STL Algorithms

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

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

    std::sort(numbers.begin(), numbers.end());

    std::cout << "Sorted array: ";
    for (int num : numbers) {
        std::cout << num << " ";
    }
    std::cout << std::endl;

    auto max_element = std::max_element(numbers.begin(), numbers.end());
    std::cout << "Max element: " << *max_element << std::endl;

    return 0;
}

Explanation

Example 3: Bounds Checking

#include <iostream>
#include <array>
#include <stdexcept>

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

    try {
        std::cout << arr.at(2) << std::endl; // OK
        std::cout << arr.at(3) << std::endl; // Throws out_of_range
    } catch (const std::out_of_range& e) {
        std::cerr << "Out of Range error: " << e.what() << std::endl;
    }

    return 0;
}

Explanation

Additional Considerations

Summary

std::array provides a safer and more convenient alternative to C-style arrays in C++. It combines the efficiency of built-in arrays with the benefits of STL containers, such as knowing its own size, supporting iteration, and working seamlessly with STL algorithms.

Key points to remember:

  1. Fixed size, known at compile time
  2. No dynamic allocation, stored on the stack if declared locally
  3. Supports STL-style iteration and algorithms
  4. Provides bounds checking with the at() method
  5. Zero overhead compared to C-style arrays

By using std::array, C++ developers can write more robust and type-safe code when working with fixed-size arrays, while maintaining the performance characteristics of built-in arrays.

Citations:

[1] https://en.cppreference.com/w/cpp/container/array [2] https://www.programiz.com/cpp-programming/std-array [3] https://en.cppreference.com/w/cpp/header/array [4] https://stackoverflow.com/questions/26614983/which-headers-in-the-c-standard-library-are-guaranteed-to-include-another-head [5] https://learn.microsoft.com/en-us/cpp/standard-library/array-class-stl?view=msvc-170 [6] https://www.geeksforgeeks.org/stdarray-in-cpp/ [7] https://embeddedartistry.com/blog/2017/06/28/an-introduction-to-stdarray/ [8] https://stackoverflow.com/questions/13131182/c-c-array-variable-in-function-header

Previous Page | Course Schedule | Course Content