Arrays

reading

Cyganek section 3.10

Arrays have many similarities to vectors, but there are some key differences:

  • Vectors can grow in size as needed to accommodate additional data. In contrast, arrays are fixed-size.

  • Arrays also are allocated in a different part of memory by default (the stack rather than the heap – more on this later).

  • Arrays do not initialize their elements by default when declared.

Here’s a simple example:

Listing 13 simple_array.cpp
#include <iostream>
#include <array>

int main() {

    std::array<int, 10> int_arr;

    for (auto e : int_arr) {
        std::cout << e << std::endl;
    }
}

Declaring the array takes 2 arguments in the <> – the datatype and the number of elements.

Notice that when we look over the elements, they are uninitialized.

We can use an initializer list to give initial values, like:

std::array<int, 10> int_arr{0};

If you don’t give all the values, the remainder are initialized to 0:

try it…

What are the values from this:

std::array<int, 10> int_arr{1, 2};

We can use the same set of algorithms we saw with vectors on arrays, like sort, find, etc.

Multidimensional Arrays

Just like with vectors, we can have an array of arrays. This would again be fixed-size, so we’ll need to specify both the number of rows and the number of columns at compile time.

Here’s an example:

Listing 14 multid_array.cpp
#include <iostream>
#include <array>
#include <iomanip>

enum ArrayDims {kcols = 4,
                krows = 3};

using RowArray = std::array<double, ArrayDims::kcols>;

using FixedSizedMatrix = std::array<RowArray, ArrayDims::krows>;

int main() {

    FixedSizedMatrix M{0.0};

    double val{0.0};
    for (auto &r : M) {
        for (auto &c : r) {
            c = val++;
        }
    }

    for (auto r : M) {
        for (auto c : r) {
            std::cout << std::setw(4) << c << " ";
        }
        std::cout << std::endl;
    }
}

There are a few features here that we have not yet seen.

  • An enum is a set of constant integers (that can be initialized in a sequence). They are contained in a namespace allowing us to access them using the scope operator (::).

  • We access the information in the arrays using a reference (with the & operator). This gives us direct access to the memory without needing to make a copy.

We’ll cover references next.

Note

In C and older C++, you will see fixed-side arrays declared as:

double x[10];
int p[10][20];

For a nice discussion of the differences between C-arrays and std::array see: https://coders-corner.net/2018/06/16/stdarray-vs-c-style-array/