Fixed-size arrays
In this lesson we'll briefly explain what is std::array
. Later in the course you'll learn more about it.
Motivationโ
We already have vectors, which represent a dynamic array, and we can use them to store any number of elements. You may ask:
Why do we even need arrays of fixed size?
The short answer is: performance.
array
vs vector
vs C-style arraysstd::array
is a more performant alternative to std::vector
, but with a few limitations. It is also a much safer alternative to C-style arrays.
We, the creators of this course are fully aware that for beginners a tiny performance improvement over vector
is not worth the extra effort.
This container will be covered more in-depth later, when you'll be more familiar with C++.
Using std::array
โ
The first thing we have to do is to include the <array>
header file:
#include <array>
Creating an arrayโ
There are quite a few ways to create an instance of std::array
.
Remember: we're talking about an array of fixed-size that we have to know in advance. We can't change the size of the array after it's created.
A common practice is to estimate the largest number of elements we'll use and then create an array of that size.
std::array<std::string, 10> player_names;
In the previous lesson we used similar notation to create vector
instance. This time we have to provide both:
- the type of the elements (
Type
) - the size of the array (
N
)
and we do that by separating them with a comma:
// prism-push-types:Type
std::array<Type, N>
It's quite usual to initialize an array with some predefined values. We can do that by using the curly braces syntax:
std::array<std::string, 10> player_names = {
"HappyBanana", "AngryCrab", "SadWolf",
};
Alternative syntax
You can omit the =
between the player_names
and the curly braces then wrap the list of elements
in an additional {}
to achieve the same result:
std::array<std::string, 10> player_names {{
"HappyBanana", "AngryCrab", "SadWolf",
}};
{ }
If you don't provide initial values for all elements, the remaining ones will be initialized with a default or zero value.
In simple terms, depending on the type inside an array: numeric values will be set to 0
, booleans to false
, and strings will be empty.
This is the content of the array after the previous code snippet:
Index | Value |
---|---|
0 | "HappyBanana" |
1 | "AngryCrab" |
2 | "SadWolf" |
3 | empty |
... | ... |
9 | empty |
Accessing elementsโ
We can access elements of an array the same way we do with vectors:
#include <iostream>
#include <string>
#include <array>
int main()
{
std::array< std::string, 10 > player_names = {
"HappyBanana", "AngryCrab", "SadWolf"
};
// Setting names of the players
player_names[0] = "SadBanana";
player_names[2] = "HappyWolf";
// Printing the name of the first player:
std::cout << "Name of the first player: " << player_names[0];
}
We changed the first and the third element of the array. The output of the program is:
Name of the first player: SadBanana
This is the content of the array after the previous code snippet:
Index | Value |
---|---|
0 | "SadBanana" |
1 | "AngryCrab" |
2 | "HappyWolf" |
3 | empty |
... | ... |
9 | empty |
Reading the size of an arrayโ
We can use the size()
method to get the size of an array:
std::array<std::string, 10> player_names = {
"HappyBanana", "AngryCrab", "SadWolf",
};
std::cout << "Number of players: " << player_names.size();
The output of the program is:
Number of players: 10
std::array
As you can see from the example above, all of array "slots" are used regardless of the number of elements we set manually. The rest are initialized with default values, which in this case are empty strings.
Displaying elementsโ
Just like we did with vectors, we can use a for
loop to display all elements of an array:
for (std::string name : player_names)
{
std::cout << "Player: " << name << '\n';
}
See full code
#include <iostream>
#include <string>
#include <array>
int main()
{
std::array< std::string, 10 > player_names = {
"HappyBanana", "AngryCrab", "SadWolf"
};
// Printing all player names
for (std::string name : player_names)
{
std::cout << name << '\n';
}
}
The output of the program is:
Player: HappyBanana
Player: AngryCrab
Player: SadWolf
Player:
Player:
Player:
Player:
Player:
Player:
Player:
Summaryโ
To sum up, we've learned that there is a fixed-size array in C++ called std::array
. In many cases we can use it similar to
a vector
, however it has some limitations and it's not as flexible. Our recommendation for you is to stick with std::vector
for now.
We'll leave it here for now, but we'll come back to it later in the course.