Standard library containers and algorithms
We have already discussed some of the containers from the C++ library, such as std::vector
and std::array
, in previous chapters. As std::vector
relies on dynamic memory allocation, std::array
is usually the container of choice in embedded applications.
Array
Arrays from the standard library allocate a contiguous block of memory on the stack. We can consider an array as a simple wrapper of a C-style array that contains the size of the array inside the type. It is a templated type that is instantiated with an underlying data type and size.
We can access members of the array using a method that will throw an exception if indexed with an out-of-bounds index. This makes it a safer option than a C-style array as it allows us to catch out-of-bounds access runtime errors and handle them. If exceptions are disabled, we can set a global terminate handler with our functionality. We had the opportunity to see this in Chapter 2 of this book...