Skip to main content

Introduction

When writing programs, you will often come across situations where you need to work with multiple variables that are of the same type. That is where arrays come in. In this lesson, we will explore the use of arrays and how they can simplify your code.

We will first cover the easiest way, using the std::vector container.

Motivationโ€‹

Let's assume that we want to store the nicknames of players who are on a game server. Each name is essentially a text, so we will store it inside a variable of type std::string.

#include <iostream>
#include <string>

int main()
{
std::string player_name1;
std::string player_name2;
std::string player_name3;

std::cout << "Please enter the name of player 1: ";
std::cin >> player_name1;

std::cout << "Please enter the name of player 2: ";
std::cin >> player_name2;

std::cout << "Please enter the name of player 3: ";
std::cin >> player_name3;
}

This code is only capable of handling exactly three players. In a situation where we have 5 players wanting to play, we will have to write the same lines of code a couple of times more. Doing this will quickly become tiring and is generally a bad idea. Instead, we will introduce an array.

What is an array?โ€‹

An array containing car brand names

An array is a container that can store some number of elements of a certain type. Look at the picture above. Inside the array we put names of vehicle brands. Note that each element has its own unique index.

Array indices

Elements of an array are indexed from zero upwards. The first element has an index 0, the second is at 1, third at 2 and so on.

Indices are necessary to distinguish array elements. Using them, we can specify which element we want to access.

Arrays in C++โ€‹

There are a few ways to create an array in C++:

Safe โœ…:

Unsafe โŒ:

  • a dynamic C-style array (e.g int* array = new int[5])
  • a fixed-size C-style array (e.g int array[5])
Unsafe and non-standard โŒ
  • a variable-length C-style array (e.g int array[UserInput])

We will focus on the first option, because it is safe and easy to use. We'll also briefly mention the second option, but you'll find a more detailed explanation later in the course. Then we will talk briefly about the C-style arrays, which are not recommended to use.

Were you told to use C-style arrays?

C-style arrays are unfortunately still often taught in schools and universities. There are a few reasons for that, which we won't discuss here. C++ is a language that is constantly evolving, and the C-style arrays were replaced by the safer alternatives mentioned above.

With that said, I should mention that a C-style array is not a bad thing in general. A programmer that is fully aware of issues that can occur when using them, and is able to avoid them, can use them safely. However, this is not the case for beginners. There are a lot of problems you can encounter when using them, and it is very easy to make a mistake. Keep in mind that std::vector and std::array cover 100% of use cases of C-style arrays, meaning that there is no situation where not using C-style array would stand in your way.

Fixed-size vs dynamic arraysโ€‹

A fixed-size array has a constant number of elements, which has to be known at compile time. This means that we cannot read the number of elements needed from a user and then create the array of exactly that size. We have to specify it directly in the code. The std::array is used for this purpose.

A dynamic array on the other hand can grow and shrink as needed. In C++ we use std::vector for this purpose.

The limitations of fixed-size arrays we mentioned above make it harder to use than dynamic arrays. Because of that, we will focus on the vector in the following lessons.

Common operationsโ€‹

There are a lot of things you can do with an array. We'll go through the most common operations that will cover most of your needs:

  1. creating an array
  2. accessing elements
  3. adding elements
  4. reading the number of elements
  5. removing elements
  6. displaying its contents

Later in the course you'll learn how to run built-in algorithms such as sorting, searching, transforming and so on.

Introduction

When writing programs, you will often come across situations where you need to work with multiple variables that are of the same type. That is where arrays come in. In this lesson, we will explore the use of arrays and how they can simplify your code.

We will first cover the easiest way, using the std::vector container.

Motivationโ€‹

Let's assume that we want to store the nicknames of players who are on a game server. Each name is essentially a text, so we will store it inside a variable of type std::string.

#include <iostream>
#include <string>

int main()
{
std::string player_name1;
std::string player_name2;
std::string player_name3;

std::cout << "Please enter the name of player 1: ";
std::cin >> player_name1;

std::cout << "Please enter the name of player 2: ";
std::cin >> player_name2;

std::cout << "Please enter the name of player 3: ";
std::cin >> player_name3;
}

This code is only capable of handling exactly three players. In a situation where we have 5 players wanting to play, we will have to write the same lines of code a couple of times more. Doing this will quickly become tiring and is generally a bad idea. Instead, we will introduce an array.

What is an array?โ€‹

An array containing car brand names

An array is a container that can store some number of elements of a certain type. Look at the picture above. Inside the array we put names of vehicle brands. Note that each element has its own unique index.

Array indices

Elements of an array are indexed from zero upwards. The first element has an index 0, the second is at 1, third at 2 and so on.

Indices are necessary to distinguish array elements. Using them, we can specify which element we want to access.

Arrays in C++โ€‹

There are a few ways to create an array in C++:

Safe โœ…:

Unsafe โŒ:

  • a dynamic C-style array (e.g int* array = new int[5])
  • a fixed-size C-style array (e.g int array[5])
Unsafe and non-standard โŒ
  • a variable-length C-style array (e.g int array[UserInput])

We will focus on the first option, because it is safe and easy to use. We'll also briefly mention the second option, but you'll find a more detailed explanation later in the course. Then we will talk briefly about the C-style arrays, which are not recommended to use.

Were you told to use C-style arrays?

C-style arrays are unfortunately still often taught in schools and universities. There are a few reasons for that, which we won't discuss here. C++ is a language that is constantly evolving, and the C-style arrays were replaced by the safer alternatives mentioned above.

With that said, I should mention that a C-style array is not a bad thing in general. A programmer that is fully aware of issues that can occur when using them, and is able to avoid them, can use them safely. However, this is not the case for beginners. There are a lot of problems you can encounter when using them, and it is very easy to make a mistake. Keep in mind that std::vector and std::array cover 100% of use cases of C-style arrays, meaning that there is no situation where not using C-style array would stand in your way.

Fixed-size vs dynamic arraysโ€‹

A fixed-size array has a constant number of elements, which has to be known at compile time. This means that we cannot read the number of elements needed from a user and then create the array of exactly that size. We have to specify it directly in the code. The std::array is used for this purpose.

A dynamic array on the other hand can grow and shrink as needed. In C++ we use std::vector for this purpose.

The limitations of fixed-size arrays we mentioned above make it harder to use than dynamic arrays. Because of that, we will focus on the vector in the following lessons.

Common operationsโ€‹

There are a lot of things you can do with an array. We'll go through the most common operations that will cover most of your needs:

  1. creating an array
  2. accessing elements
  3. adding elements
  4. reading the number of elements
  5. removing elements
  6. displaying its contents

Later in the course you'll learn how to run built-in algorithms such as sorting, searching, transforming and so on.