Skip to content

Latest commit

 

History

History
55 lines (40 loc) · 3.18 KB

std_array.md

File metadata and controls

55 lines (40 loc) · 3.18 KB

std::array is a container introduced in C++11 that encapsulates fixed-size arrays. Unlike built-in arrays, std::array doesn't decay into a pointer when passed to a function, and it also provides several member functions that make it more convenient to work with. Here's why you might prefer to use std::array:

Arrays are fixed-size sequence containers: they hold a specific number of elements ordered in a strict linear sequence.

Advantages

  • Type Safety: std::array includes the size as part of its type, so it's safer in terms of type checking. This prevents issues related to array decay and accidental changes to the array's size.

  • Standard Container Interface: std::array provides functions that are consistent with other C++ standard containers (like size(), begin(), end()), making it more consistent and easier to use with algorithms from the standard library.

  • No Performance Overhead: std::array doesn't have any overhead compared to a built-in array. It's simply a wrapper around the built-in array, and modern compilers can optimize it just as well.

  • Bounds Checking: Using the at() member function, you can access elements with bounds checking, which can make your code safer.

Passing std::array by Reference

When you pass std::array to a function, it behaves differently than a built-in array. If you pass it by value, a copy of the array is made. This can be inefficient if the array is large since copying a large block of memory can be time-consuming.

By passing the std::array by reference, you avoid this copy, making the code more efficient. Here's an example:

#include <array>

void modifyArray(std::array<int, 5>& arr) {
    arr[0] = 99; // Modifying the original array, not a copy
}

int main() {
    std::array<int, 5> numbers = {10, 20, 30, 40, 50};
    modifyArray(numbers); // Passes by reference
    // numbers[0] is now 99
    return 0;
}

If you want to ensure that the function doesn't modify the std::array, you can pass it as a const reference:

void printArray(const std::array<int, 5>& arr) {
    // ...
}

Functions for std::array

  • size(): Returns the number of elements in the array. Since std::array has a fixed size, this value is always equal to the size specified at the type definition.
  • max_size(): Returns the maximum number of elements the array can hold, which is the same as size() for std::array.
  • empty(): Returns a boolean indicating whether the array is empty. Always returns false for std::array unless the size is 0.
  • at(index): Provides access to the element at the specified index, with bounds checking.
  • operator[]: Provides access to the element at the specified index, without bounds checking.
  • front(): Returns a reference to the first element in the array.
  • back(): Returns a reference to the last element in the array.
  • data(): Returns a pointer to the underlying raw array.
  • fill(value): Assigns the given value to all elements in the array.
  • swap(other_array): Exchanges the content of the array with another array of the same size and type.
  • begin(), end(): Provide iterators for iterating over the elements.