Chapter 6 – C++11 features: Part 2

Enum class

With enums, we can have an enumeration over some items.

The enum class is not convertible to int, we need to use its scope::name.

The name of an enum id does not collide with other names in the same scope, because they exist in a different scope (in the scope of the enum class).

Containers

forward list

Forward list was added in C11. List was already there prior, but it was implemented as a double-linked list, which was taken too much memory if the developer only required a single-linked list.

Vector

Vector is an array of values that dynamically allocate memory as needed.

Array

Arrays are similar to vectors, but the memory is static and need to be given upfront.

std::array:fill is a function that assigns a value to all the elements in the container.

std::array::get<X>(arr) where X = position, arr = container

  • returns the element of the container at position X.

X must be an integer value in range [0, N). This is enforced at compile time as opposed to at() or operator[].

Tuple

Tuple is a container that can store multiple types, similar to defining a structure with multiple types in it.

std::get is also used to fetch data from a tuple.

Algorithms

There are many algorithms added in C++11, and they work great with lambda functions.

std::accumulate

Accumulate iterate through a container and sum the values, or multiply them as needed.

std::transform

Transform iterate through a container and modify the values one by one.

std::count

Count iterate through a container and count how many values are equal to the value received as input.

std::any_of

any_of iterate through a container and returns true if at least one object satisfies the condition.

std::all_of

all_of iterate through a container and returns true if all objects from it satisfies the given condition.

You may also like...