C++ Maps

C++ Map

A map stores elements in “key/value” pairs.

Elements in a map are:

  • Accessible by keys (not index), and each key is unique.
  • Automatically sorted in ascending order by their keys.

To use a map, you have to include the <map> header file:

// Include the map library
#include <map>

Continue reading C++ Maps

C++ Sets

C++ Set

A set stores unique elements where they:

  • Are sorted automatically in ascending order.
  • Are unique, meaning equal or duplicate values are ignored.
  • Can be added or removed, but the value of an existing element cannot be changed.
  • Cannot be accessed by index numbers, because the order is based on sorting and not indexing.

To use a set, you have to include the <set> header file:

// Include the set library
#include <set>

Continue reading C++ Sets

C++ Deque

C++ Deque

In the previous page, your learned that elements in a queue are added at the end and removed from the front.

A deque (stands for double-ended queue) however, is more flexible, as elements can be added and removed from both ends (at the front and the back). You can also access elements by index numbers.

To use a deque, you have to include the <deque> header file:

// Include the deque library
#include <deque>

Continue reading C++ Deque

C++ Queues

C++ Queue

A queue stores multiple elements in a specific order, called FIFO.

FIFO stands for First in, First Out. To visualize FIFO, think of a queue as people standing in line in a supermarket. The first person to stand in line is also the first who can pay and leave the supermarket. This way of organizing elements is called FIFO in computer science and programming.

Unlike vectors, elements in the queue are not accessed by index numbers. Since queue elements are added at the end and removed from the front, you can only access an element at the front or the back.

To use a queue, you have to include the <queue> header file:

// Include the queue library
#include <queue>

Continue reading C++ Queues

C++ Stacks

C++ Stack

A stack stores multiple elements in a specific order, called LIFO.

LIFO stands for Last in, First Out. To vizualise LIFO, think of a pile of pancakes, where pancakes are both added and removed from the top. So when removing a pancake, it will always be the last one you added. This way of organizing elements is called LIFO in computer science and programming.

Unlike vectors, elements in the stack are not accessed by index numbers. Since elements are added and removed from the top, you can only access the element at the top of the stack. Continue reading C++ Stacks

C++ List

C++ List

A list is similar to a vector in that it can store multiple elements of the same type and dynamically grow in size.

However, two major differences between lists and vectors are:

    1. You can add and remove elements from both the beginning and at the end of a list, while vectors are generally optimized for adding and removing at the end.

 

  1. Unlike vectors, a list does not support random access, meaning you cannot directly jump to a specific index, or access elements by index numbers.

Continue reading C++ List

C++ Vectors

C++ Vector

A vector in C++ is like a resizable array.

Both vectors and arrays are data structures used to store multiple elements of the same data type.

The difference between an array and a vector, is that the size of an array cannot be modified (you cannot add or remove elements from an array). A vector however, can grow or shrink in size as needed.

To use a vector, you have to include the <vector> header file:

// Include the vector library
#include <vector>

Continue reading C++ Vectors

C++ Data Structures and STL

Data Structures

Data structures are used to store and organize data. An array is an example of a data structure, which allows multiple elements to be stored in a single variable.

C++ includes many other data structures as well, each is used to handle data in different ways.

These are part of the C++ STL, which stands for The Standard Template Library.

Continue reading C++ Data Structures and STL

C++ Date and Time

Date and Time

The <ctime> library allows us to work with dates and times.

To use it, you must import the <ctime> header file:

Example

#include <ctime> // Import the ctime library

Continue reading C++ Date and Time

C++ Exceptions

C++ Exceptions

When executing C++ code, different errors can occur: coding errors made by the programmer, errors due to wrong input, or other unforeseeable things.

When an error occurs, C++ will normally stop and generate an error message. The technical term for this is: C++ will throw an exception (throw an error).

Continue reading C++ Exceptions