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
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
std::cout << "Please enter the name of player 1: ";
std::cin >> playerName1;
std::cout << "Please enter the name of player 2: ";
std::cin >> playerName2;
std::cout << "Please enter the name of player 3: ";
std::cin >> playerName3;
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 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.
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++:
- a dynamic C-style array (e.g
int* array = new int)
- a fixed-size C-style array (e.g
Unsafe and non-standard ❌
- a variable-length C-style array (e.g
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::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
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.
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:
- creating an array
- accessing elements
- adding elements
- reading the number of elements
- removing elements
- displaying its contents
Later in the course you'll learn how to run built-in algorithms such as sorting, searching, transforming and so on.