std::array : safe C arrays

The std::array was a new container introduced in C++11. A std::array works in the same way as a plain old C array (like int values[5]) but has the advantage that it will not decay into a plain pointer.

Unlike the vector can an array not be resized once it has been created.

Creating

std::array is always allocated on the stack. It it allocated using uniform initialization:

Iterating

You can iterate through each of the values in the array using range-based for:

or using std::for_each and a lambda

Facebookgoogle_pluslinkedinmail

std::map : The C++ dictionary class

std::map is the C++ standard library implementation of  a dictionary. It contains key-value pairs where the keys are all unique and the key values are used to find the associated values, much like a real-world dictionary. The map is, just like most of the standard library, implemented using templates and the types of the keys and the values are given when you create the map. Some examples of maps:

Each element of the map is of the type std::map<K, V>::value_type which is just a redefinition of std::pair<const K, V>.

Getting one element

To get the value associated with a specific key, use the bracket operator ([]). This operator will create the key-value pair if it doesn’t exist already.

Checking if a key exists in map

There are two ways to check if a key exists in a map.

  1. Using the function std::map::count(). This returns the number of keys which matches the queried key, and since all keys in the std::map are unique can this only be zero or one. This is a good way to do it if you just need to see if the key exists and don’t need to do anything with the map.
  2. Using the function std::map::find(). This returns an iterator to the found object, or std::map::end() if the object is not found. This is a better choice if you need to update the map, since you will get an iterator to the found key value pair.

Iteration

To iterate over all elements in the dictionary use range-based for:

or much simpler using auto

Notice that it is a good idea to make the variable in the loop a reference since this avoids copying the element and gives better performance. Also if you don’t intend to change the element then you can make this clear by also making it const.

Erasing one element

You can erase one element from the map by using the function erase(…) which takes either an iterator, a key or a two iterators defining a range of values to remove.

 

Facebookgoogle_pluslinkedinmail

Range-based for

With C++11 we got a new way of writing for-loops to iterate over all elements in a range. The new range-based for loop is much clearer than the old way using iterators. It is intended to be used when you only look at the elements in a container, you are not supposed to change the container itself since this could cause problems in the loop.

An example of how the new range-based for works, using a vector as an example:

When the vector contains an object then it’s a good idea to get a reference to the object to avoid copying the entire object in each round in the loop.

If the contents of the vector shouldn’t be changed then we can show this by getting a const reference:

We can use the auto keyword to avoid explicitly giving the type:

 

Facebookgoogle_pluslinkedinmail