std::array

std::array is a container provided by the Standard Template Library (STL) that represents a fixed-size array.

It is a more modern alternative to the built-in C-style arrays and provides several additional features, such as bounds checking, size retrieval, and compatibility with other STL algorithms and containers.

Creating a std::array:


To use std::array, you need to include the <array> header.

#include <iostream>
#include <array>

int main() {
    // Create an empty std::array of integers with size 5
    std::array<int, 5> myArray;

    // Create a std::array and initialize using initializer list
    std::array<int, 5> initializedArray = {10, 20, 30, 40, 50};

    return 0;
}

Advantages of std::array:

  1. Fixed-size: The size of the array is determined at compile-time and cannot be changed during runtime, providing better safety and performance compared to dynamic containers like std::vector.
  2. Efficient element access: Elements are stored in contiguous memory, allowing constant-time access using indexing (with no overhead for boundary checking).
  3. Compatible with STL algorithms: std::array is compatible with various STL algorithms and can be used as a drop-in replacement for traditional C-style arrays.

Disadvantages of std::array:

  1. Fixed-size: The size of the array is fixed at compile-time, which might be a limitation when the size is not known in advance or needs to change dynamically.
  2. Lack of dynamic resizing: Unlike std::vector, std::array cannot dynamically resize to accommodate a changing number of elements.

std::array is useful when you know the size of the container at compile-time and need efficient access to elements using indexing. It is especially useful in cases where compatibility with STL algorithms is required and where you need the safety and performance advantages of a fixed-size container. However, if you need a dynamic container that can change its size during runtime, you should consider using std::vector or other dynamic containers provided by the STL.

Important Functions of std::array:

size():

Returns the number of elements in the array.

  • Time Complexity: Constant time (O(1)).
  • Space Complexity: Constant space (O(1)).
#include <iostream>
#include <array>

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

    std::cout << "Size of the array: " << myArray.size() << std::endl; // Output: 5

    return 0;
}

empty():

Checks if the array is empty.

  • Time Complexity: Constant time (O(1)).
  • Space Complexity: Constant space (O(1)).
#include <iostream>
#include <array>

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

    if (myArray.empty()) {
        std::cout << "Array is empty." << std::endl;
    } else {
        std::cout << "Array is not empty." << std::endl;
    }

    return 0;
}

at(index):

Returns a reference to the element at the specified index, with bounds checking.

  • Time Complexity: Constant time (O(1)).
  • Space Complexity: Constant space (O(1)).
#include <iostream>
#include <array>

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

    int element = myArray.at(2); // element = 30

    return 0;
}

operator[]:

Returns a reference to the element at the specified index, without bounds checking (be cautious with out-of-bounds access).

  • Time Complexity: Constant time (O(1)).
  • Space Complexity: Constant space (O(1)).
#include <iostream>
#include <array>

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

    int element = myArray[2]; // element = 30

    return 0;
}

front():

Returns a reference to the first element in the array.

  • Time Complexity: Constant time (O(1)).
  • Space Complexity: Constant space (O(1)).
#include <iostream>
#include <array>

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

    int firstElement = myArray.front(); // firstElement = 10

    return 0;
}

back():

Returns a reference to the last element in the array.

  • Time Complexity: Constant time (O(1)).
  • Space Complexity: Constant space (O(1)).
#include <iostream>
#include <array>

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

    int lastElement = myArray.back(); // lastElement = 50

    return 0;
}

fill(value):

Fills the entire array with the specified value.

  • Time Complexity: Linear time (O(n)), where n is the size of the array.
  • Space Complexity: Constant space (O(1)).
#include <iostream>
#include <array>

int main() {
    std::array<int, 5> myArray;

    myArray.fill(42); // After this, myArray contains {42, 42, 42, 42, 42}

    return 0;
}

begin():

Returns an iterator to the beginning of the array.

  • Time Complexity: Constant time (O(1)).
  • Space Complexity: Constant space (O(1)).
#include <iostream>
#include <array>

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

    auto it = myArray.begin();
    std::cout << "First element: " << *it << std::endl; // Output: 10

    return 0;
}

end():

Returns an iterator to the end of the array.

  • Time Complexity: Constant time (O(1)).
  • Space Complexity: Constant space (O(1)).
#include <iostream>
#include <array>

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

    auto it = myArray.end();
    --it; // Move iterator to the last element
    std::cout << "Last element: " << *it << std

::endl; // Output: 50

    return 0;
}

rbegin():

Returns a reverse iterator to the beginning of the reversed array.

  • Time Complexity: Constant time (O(1)).
  • Space Complexity: Constant space (O(1)).
#include <iostream>
#include <array>

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

    auto it = myArray.rbegin();
    std::cout << "First element (reversed): " << *it << std::endl; // Output: 50

    return 0;
}

rend():

Returns a reverse iterator to the end of the reversed array.

  • Time Complexity: Constant time (O(1)).
  • Space Complexity: Constant space (O(1)).
#include <iostream>
#include <array>

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

    auto it = myArray.rend();
    --it; // Move iterator to the last element in the reversed array
    std::cout << "Last element (reversed): " << *it << std::endl; // Output: 10

    return 0;
}