diff --git a/content/learn/course/basics/arrays/_codes/dynamic-arrays/adding-element.mdx b/content/learn/course/basics/arrays/_codes/dynamic-arrays/adding-element.mdx new file mode 100644 index 000000000..05ebfa629 --- /dev/null +++ b/content/learn/course/basics/arrays/_codes/dynamic-arrays/adding-element.mdx @@ -0,0 +1,23 @@ +```cpp +#include <iostream> +#include <string> +#include <vector> + +int main() +{ + using std::vector, std::string, std::cout; + + vector<string> playerNames = { + "HappyBanana", + "AngryCrab", + "SadWolf", + }; + + // Adding a new player: + // highlight-next-line + playerNames.push_back("WickedWitch"); + + // Printing the name of player with index 3 + cout << "Name of the player with index 3: " << playerNames[3]; +} +``` \ No newline at end of file diff --git a/content/learn/course/basics/arrays/_codes/dynamic-arrays/displaying-array-of-player-names-auto.mdx b/content/learn/course/basics/arrays/_codes/dynamic-arrays/displaying-array-of-player-names-auto.mdx index 1bcb6c687..c2264757d 100644 --- a/content/learn/course/basics/arrays/_codes/dynamic-arrays/displaying-array-of-player-names-auto.mdx +++ b/content/learn/course/basics/arrays/_codes/dynamic-arrays/displaying-array-of-player-names-auto.mdx @@ -7,12 +7,11 @@ int main() { using std::vector, std::string, std::cout; - vector< string > playerNames(3); - - // Setting names of the players - playerNames[0] = "HappyBanana"; - playerNames[1] = "AngryCrab"; - playerNames[2] = "SadWolf"; + vector<string> playerNames = { + "HappyBanana", + "AngryCrab", + "SadWolf", + }; // Adding a new player: playerNames.push_back("WickedWitch"); diff --git a/content/learn/course/basics/arrays/_codes/dynamic-arrays/displaying-array-of-player-names.mdx b/content/learn/course/basics/arrays/_codes/dynamic-arrays/displaying-array-of-player-names.mdx index cce2ae3bb..98dec132d 100644 --- a/content/learn/course/basics/arrays/_codes/dynamic-arrays/displaying-array-of-player-names.mdx +++ b/content/learn/course/basics/arrays/_codes/dynamic-arrays/displaying-array-of-player-names.mdx @@ -7,12 +7,11 @@ int main() { using std::vector, std::string, std::cout; - vector< string > playerNames(3); - - // Setting names of the players - playerNames[0] = "HappyBanana"; - playerNames[1] = "AngryCrab"; - playerNames[2] = "SadWolf"; + vector<string> playerNames = { + "HappyBanana", + "AngryCrab", + "SadWolf", + }; // Adding a new player: playerNames.push_back("WickedWitch"); diff --git a/content/learn/course/basics/arrays/_codes/dynamic-arrays/erasing-element.mdx b/content/learn/course/basics/arrays/_codes/dynamic-arrays/erasing-element.mdx index 6441db7f0..1a006b198 100644 --- a/content/learn/course/basics/arrays/_codes/dynamic-arrays/erasing-element.mdx +++ b/content/learn/course/basics/arrays/_codes/dynamic-arrays/erasing-element.mdx @@ -7,12 +7,11 @@ int main() { using std::vector, std::string, std::cout; - vector< string > playerNames(3); - - // Setting names of the players - playerNames[0] = "HappyBanana"; - playerNames[1] = "AngryCrab"; - playerNames[2] = "SadWolf"; + vector<string> playerNames = { + "HappyBanana", + "AngryCrab", + "SadWolf", + }; // Adding a new player: playerNames.push_back("WickedWitch"); diff --git a/content/learn/course/basics/arrays/_codes/dynamic-arrays/initializing-vector.mdx b/content/learn/course/basics/arrays/_codes/dynamic-arrays/initializing-vector.mdx new file mode 100644 index 000000000..f92c5c835 --- /dev/null +++ b/content/learn/course/basics/arrays/_codes/dynamic-arrays/initializing-vector.mdx @@ -0,0 +1,21 @@ +```cpp +#include <iostream> +#include <string> +#include <vector> + +int main() +{ + using std::vector, std::string, std::cout; + + // highlight-start + vector<string> playerNames = { + "HappyBanana", + "AngryCrab", + "SadWolf", + }; + // highlight-end + + // Printing the name of the first player: + cout << "Name of the first player: " << playerNames[0]; +} +``` \ No newline at end of file diff --git a/content/learn/course/basics/arrays/_codes/dynamic-arrays/inserting-element.mdx b/content/learn/course/basics/arrays/_codes/dynamic-arrays/inserting-element.mdx index 226eb8a04..ea39440d9 100644 --- a/content/learn/course/basics/arrays/_codes/dynamic-arrays/inserting-element.mdx +++ b/content/learn/course/basics/arrays/_codes/dynamic-arrays/inserting-element.mdx @@ -7,12 +7,11 @@ int main() { using std::vector, std::string, std::cout; - vector< string > playerNames(3); - - // Setting names of the players - playerNames[0] = "HappyBanana"; - playerNames[1] = "AngryCrab"; - playerNames[2] = "SadWolf"; + vector<string> playerNames = { + "HappyBanana", + "AngryCrab", + "SadWolf", + }; // Adding a new player: playerNames.push_back("WickedWitch"); diff --git a/content/learn/course/basics/arrays/_codes/dynamic-arrays/reading-size.mdx b/content/learn/course/basics/arrays/_codes/dynamic-arrays/reading-size.mdx index 54ff85c71..a0582da2e 100644 --- a/content/learn/course/basics/arrays/_codes/dynamic-arrays/reading-size.mdx +++ b/content/learn/course/basics/arrays/_codes/dynamic-arrays/reading-size.mdx @@ -7,12 +7,11 @@ int main() { using std::vector, std::string, std::cout; - vector< string > playerNames(3); - - // Setting names of the players - playerNames[0] = "HappyBanana"; - playerNames[1] = "AngryCrab"; - playerNames[2] = "SadWolf"; + vector<string> playerNames = { + "HappyBanana", + "AngryCrab", + "SadWolf", + }; // highlight-next-line cout << "The array contains " << playerNames.size() << " elements\n"; @@ -26,10 +25,6 @@ int main() playerNames.insert(playerNames.begin() + 1, "BadPenguin"); // Erase the first element: - // highlight-next-line playerNames.erase(playerNames.begin() + 0); - - // highlight-next-line - cout << "The array contains " << playerNames.size() << " elements\n"; } ``` \ No newline at end of file diff --git a/content/learn/course/basics/arrays/dynamic-arrays.mdx b/content/learn/course/basics/arrays/dynamic-arrays.mdx index 984556b55..1019afdd2 100644 --- a/content/learn/course/basics/arrays/dynamic-arrays.mdx +++ b/content/learn/course/basics/arrays/dynamic-arrays.mdx @@ -17,7 +17,9 @@ import ImproveSection from "@site/i18n/en/presets/ImproveSection.mdx"; <!-- Codes --> import FullCode_Motivation from "./_codes/motivation.mdx"; +import FullCode_InitializingVector from "./_codes/dynamic-arrays/initializing-vector.mdx"; import FullCode_ErasingElement from "./_codes/dynamic-arrays/erasing-element.mdx"; +import FullCode_AddingElementToArray from "./_codes/dynamic-arrays/adding-element.mdx"; import FullCode_InsertingElement from "./_codes/dynamic-arrays/inserting-element.mdx"; import FullCode_ReadingSize from "./_codes/dynamic-arrays/reading-size.mdx"; import FullCode_DisplayingArrayOfPlayerNames from "./_codes/dynamic-arrays/displaying-array-of-player-names.mdx"; @@ -25,7 +27,24 @@ import FullCode_DisplayingArrayOfPlayerNamesAuto from "./_codes/dynamic-arrays/d # Dynamic arrays -In this lesson we'll learn how to use dynamic arrays in C++ using the `std::vector` type. +In this lesson we'll learn how to use dynamic arrays in C++ using the [`std::vector`](/docs/std/containers/arrays/vector/) type. + +## About vector + +You might be wondering why the type that represents a dynamic array data structure in C++ uses the name "vector." +It can be confusing for those who associate the term with the mathematical concept of vectors. +Interestingly, this name was chosen by the original author of the STL (Standard Template Library), Alexander Stepanov. However, in retrospect, +[Stepanov admitted](https://stackoverflow.com/a/758548/4386320) that using "vector" as the name of the data structure was a mistake. + +`vector` is one of [*containers*](/docs/std/containers/) that are available in the standard library. You'll learn about other containers +gradually as you progress through the course. In this lesson I will often use the term *array* and *vector* interchangeably. + +:::tip Documentation +Once you finish this lesson you may look at the [documentation](/docs/std/containers/arrays/vector/) to find out more about the `vector`. +Be aware that the documentation is not a tutorial, but rather a reference and it may be a bit overwhelming at first. +::: + +## Creating a vector Let's go back and consider the code we've shown in the [Motivation](../introduction/#motivation) section of the previous lesson. There is a great candidate to be turned into an array. The following variables are of the same type (`std::string`) @@ -39,9 +58,7 @@ std::string playerName3; <FullCode content={<FullCode_Motivation/>} /> -Instead of making three separate variables, we can create one array that contains three elements. - -## Creating a vector +Instead of creating three separate variables, we can create one array that contains three elements. To use `std::vector` we first have to include its header file: @@ -60,7 +77,7 @@ that will be stored inside it. Keep in mind that all of the elements in a vector int main() { // highlight-next-line - std::vector< std::string > playerNames(3); + std::vector<std::string> playerNames(3); // ... } ``` @@ -76,7 +93,7 @@ int main() { // highlight-next-line using std::vector, std::string; - vector< string > playerNames(3); + vector<string> playerNames(3); } ``` @@ -98,10 +115,10 @@ vector< /*other type*/ > arrayOfXYZ; The following code defines a variable `playerNames` of a vector type that stores text elements (`std::string`s): ```cpp title="Defining a variable of vector type" -vector< string > playerNames(3); +vector<string> playerNames(3); ``` -A parenthesis with 3 in the middle will make the vector store 3 variables, or more precisely, 3 elements +Parentheses with 3 in the middle will make the vector store 3 variables, or more precisely, 3 elements of type `string` right from the beginning. ```cpp title="Storing 3 elements in the vector" @@ -112,7 +129,7 @@ Note that this is a vector-specific thing and not a general rule. If you want to just don't write the parentheses at all: ```cpp title="Creating an empty vector" -vector< string > playerNames; +vector<string> playerNames; ``` :::danger Empty parentheses problem @@ -136,7 +153,13 @@ This is why the empty parentheses turns this into a function declaration, which ## Accessing elements -Let's give players the following names: +There are two ways of accessing an element of a vector, that we're interested in: + +- using the [`[]` operator](/docs/std/containers/arrays/vector/operator_subscript/) +- calling the [`at()` method](/docs/std/containers/arrays/vector/at/) + +Both ways are very similar, with the only difference being that the `[]` operator does not check if the index is out of bounds. +We'll show you what this means in a moment. Now let's see how we can use them - we'll assign the following names to the players: | Player index | Name | | ------------- | ---- | @@ -144,6 +167,8 @@ Let's give players the following names: | 1 | AngryCrab | | 2 | SadWolf | +<br/> + Here is how we can set them in the code: ```cpp @@ -155,7 +180,7 @@ int main() { using std::vector, std::string, std::cout; - vector< string > playerNames(3); + vector<string> playerNames(3); // Setting names of the players playerNames[0] = "HappyBanana"; @@ -167,55 +192,123 @@ int main() } ``` +```console title="Output" +Name of the first player: HappyBanana +``` + To access an element of an array, we put its index in square brackets right after the array name: ```cpp arrayName[ index ] ``` -A non-empty array with number of elements equal to `N` always has indices from `0` to, -`N-1` inclusive. A three-element array `playerNames` has indices from `0` to, `2` inclusive. -An attempt to rename the player with index `3` will result in a runtime error: +A non-empty array with a number of elements equal to `N` always has indices ranging from `0` to `N-1` inclusive. +A three-element array `playerNames` has indices from `0` to `2` inclusive. + +### Out of bounds access -```cpp title="🔴 Run-time error" +A **very common** mistake is to try to access an element with an index that is out of bounds. +An attempt to rename the player with index `3` will result in an unstable behavior of the program, most likely +causing it to crash. + +```cpp // error-next-line -playerNames[3] = "NewPlayer"; // Error +playerNames[3] = "NewPlayer"; // ! ``` -The reason is that the element with index `3` does not exist (size is `3` but the last item has index `2`). -This code will compile correctly (we might get a warning), but running this program will result in an error. +The reason is that the element with index `3` does not exist (size is `3` but the last item is at index `2`). +This code will compile correctly (we might get a warning), but when the program is executed, it will try to access +a memory location that is not allocated for the program. This is called a **buffer overflow** and it is a very serious problem. -## Adding elements at the end +An alternative, safer way of accessing an element is to use the `at()` method: -To add another item to the array, we need to use `push_back` like this: +```cpp +playerNames.at( 3 ) = "NewPlayer"; +``` -```cpp title="🔹 Adding an item to the array" -#include <iostream> -#include <string> -#include <vector> +The difference is that the `at()` method checks if the index is out of bounds and throws an exception if it is. +Be aware that it won't make your program valid by itself. The key benefit is that it will show you a useful +error message and won't allow the program to perform a potentially dangerous operation. -int main() -{ - using std::vector, std::string, std::cout; +We'll learn more about the syntax we used to call the `at()` method in a moment. - vector< string > playerNames(3); +### Example - // Setting names of the players - playerNames[0] = "HappyBanana"; - playerNames[1] = "AngryCrab"; - playerNames[2] = "SadWolf"; +This is how we can ask a user to enter the name of a certain player: - // Adding a new player: - // highlight-next-line - playerNames.push_back("WickedWitch"); +```cpp +cout << "Enter the name of the second player: "; +// highlight-next-line +cin >> playerNames[1]; +``` - // Printing the name of player with index 3 - cout << "Name of the player with index 3: " << playerNames[3]; -} +Now let's output the number of characters in the provided name: + +```cpp +cout << "The name of the second player has " << playerNames[1].size() << " characters.\n"; +``` + +```console title="Possible output" +Enter the name of the second player: FuriousFlamingo +The name of the second player has 15 characters. +``` + +## Providing initial values + +There is also a way to provide initial values for the elements of the `vector` right from the beginning: + +```cpp +vector<string> playerNames = { + "HappyBanana", + "AngryCrab", + "SadWolf", +}; +``` + +<FullCode content={<FullCode_InitializingVector/>} /> + +<details> +<summary>Alternative syntax</summary> +<div> + +In the case of initializing a `vector` you can also omit the `=` sign: + +```cpp title="Alternative syntax" +// highlight-next-line +vector<string> playerNames { // note the lack of '=' + "HappyBanana", + "AngryCrab", + "SadWolf", +}; ``` +The result is the same. + +</div> +</details> + +We will use this method of initialization in the current lesson from now on. + +## Adding elements at the end + +To add an item to the end of an array (in this case `playerNames`), we need to use `push_back` like this: + +```cpp +// highlight-next-line +playerNames.push_back("WickedWitch"); + +// Printing the name of player with index 3 +cout << "Name of the player with index 3: " << playerNames[3]; +``` + +```console title="Output" +Name of the player with index 3: WickedWitch +``` + +<FullCode content={<FullCode_AddingElementToArray/>} /> + From the moment `.push_back(...)` instruction was executed, the `playerNames` array already has four elements. -We say that we *called* the `push_back(...)` *method*. We'll talk more about calls and methods in the future. +We say that we are *calling* the `push_back(...)` *method*. We'll talk more about calls and methods in the future. For now, just remember that: 1. we put a dot after the name of the array @@ -250,12 +343,25 @@ After: </div> </Columns> +<br/> + +To let the user add a new player, we can ask them to enter their name, and then add it to the array: + +```cpp +std::cout << "Enter the name of the new player: "; +std::string newPlayerName; +// highlight-start +std::cin >> newPlayerName; +playerNames.push_back(newPlayerName); +// highlight-end +``` + ## Inserting elements at a specific position At this point you will have to trust me a little. I won't go into details because it's too complicated for now. To insert **before** index `n` to a vector (in this case `playerNames`) we use the following notation: -```cpp title="Inserting an item to the array" +```cpp playerNames.insert(playerNames.begin() + n, elementToInsert); ``` @@ -265,7 +371,7 @@ which isn't the same thing as an index. To obtain it, we need to use `begin()` a Knowing this, we will now insert a new player just before the `AngryCrab` (index `1`): -```cpp title="Inserting an item to the array" +```cpp playerNames.insert(playerNames.begin() + 1, "BadPenguin"); ``` @@ -310,9 +416,9 @@ So to remove the `n`-th element from a vector (e.g. from `playerNames`) we will playerNames.erase( playerNames.begin() + n ); ``` -We'll now remove the first player from the array: +We'll now erase the first player from the array: -```cpp title="Erasing the first element from playerNames" +```cpp playerNames.erase( playerNames.begin() + 0 ); ``` @@ -353,6 +459,11 @@ After: <br/> +As you can see the first element was removed and the rest of the elements **shifted by one** index. +This is because the array is a **contiguous** block of memory and `vector` ensures that there is no +empty space left after an element is removed. + + :::danger Before erasing an item from an array, make sure it exists (that is, it's in scope `[0, N)`). @@ -370,16 +481,27 @@ else ::: +## Clearing the contents + +To clear the contents of a vector, we can use the [`clear()`](/docs/std/containers/arrays/vector/clear/) method: + +```cpp +playerNames.clear(); +``` + +After the call, the array will be empty. + ## Reading the size Because `vector` can change size whenever you want it to, you may sometimes need to read the number of elements it currently contains. The current size can be read using the `size()` method: ```cpp -// Setting names of the players -playerNames[0] = "HappyBanana"; -playerNames[1] = "AngryCrab"; -playerNames[2] = "SadWolf"; +vector<string> playerNames = { + "HappyBanana", + "AngryCrab", + "SadWolf", +}; // highlight-next-line cout << "The array contains " << playerNames.size() << " elements\n"; @@ -391,11 +513,30 @@ cout << "Added new player.\n"; cout << "The array contains " << playerNames.size() << " elements\n"; ``` +```console title="Output" +The array contains 3 elements +Added new player. +The array contains 4 elements +``` + <FullCode content={<FullCode_ReadingSize/>}/> Just like we previously did with the method `push_back`, we write the name after the dot. Then we put the parentheses, and in the case of `.size` we leave them empty. +## Checking if vector is empty + +To check if a vector is empty, we can use the `empty()` method like this: + +```cpp +if (playerNames.empty()) + cout << "The array is empty!\n"; +else + cout << "The array is not empty!\n"; +} +``` + + ## Displaying elements If we want to display **all** the elements of an array, we'll have to use a *loop*. @@ -409,6 +550,13 @@ for (string name : playerNames) } ``` +```console title="Output" +Player name: HappyBanana +Player name: AngryCrab +Player name: SadWolf +Player name: WickedWitch +``` + <FullCode content={<FullCode_DisplayingArrayOfPlayerNames/>}/> To understand this, let me show you how to "read" it: @@ -422,6 +570,7 @@ There is only one statement inside this block of code: ```cpp cout << "Player name: " << name << '\n'; ``` + The loop will write the nicknames of the players one by one into the variable `name`, and execute the display instruction (`cout`) for each of them. diff --git a/content/learn/course/basics/arrays/dynamic-arrays/examples.mdx b/content/learn/course/basics/arrays/dynamic-arrays/examples.mdx index fb6799be4..e7da6ccb3 100644 --- a/content/learn/course/basics/arrays/dynamic-arrays/examples.mdx +++ b/content/learn/course/basics/arrays/dynamic-arrays/examples.mdx @@ -5,4 +5,45 @@ tags: [vector, examples, arrays, dynamic] hide_title: true --- -# Dynamic arrays » More examples \ No newline at end of file +# Dynamic arrays » More examples + +Now that we know how to create and use dynamic arrays, let's see some more examples. + +:::note Loops +To take full advantage of what we can do with arrays, you'll need to learn about **loops**. We're going to learn about them +right after arrays. Until then in the following example we'll use only the most basic `for` loop that we've shown you already. +::: + +## Example 1: + +```cpp title="Manipulating list of student grades" +#include <iostream> +#include <vector> +#include <string> + +int main() +{ + std::vector<std::string> studentNames(3); + std::vector<int> studentGrades(3); + + // Ask user to enter student names and grades + std::cout << "Enter the first student's name: "; + std::cin >> studentNames[0]; + std::cout << "Enter the first student's grade: "; + std::cin >> studentGrades[0]; + + std::cout << "Enter the second student's name: "; + std::cin >> studentNames[1]; + std::cout << "Enter the second student's grade: "; + std::cin >> studentGrades[1]; + + std::cout << "Enter the third student's name: "; + std::cin >> studentNames[2]; + std::cout << "Enter the third student's grade: "; + std::cin >> studentGrades[2]; + + // Print out the list of students and their grades + std::cout << "Student list:" << std::endl; + +} +``` \ No newline at end of file diff --git a/content/learn/course/basics/arrays/introduction.mdx b/content/learn/course/basics/arrays/introduction.mdx index 93fe973a9..1733b0274 100644 --- a/content/learn/course/basics/arrays/introduction.mdx +++ b/content/learn/course/basics/arrays/introduction.mdx @@ -19,16 +19,14 @@ import Code_Motivation from "./_codes/motivation.mdx"; # Introduction -As we gradually do calculations on more and more data, regular variables start to be insufficient. -We will use arrays as a comfortable way to store bigger number of variables of the same type. -This lesson will teach you how to create and manipulate arrays in C++. +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, that is -using the [`std::vector`](/docs/std/containers/arrays/vector/) type. +We will first cover the easiest way, using the [`std::vector`](/docs/std/containers/arrays/vector/) type. ## Motivation -Let's assume that we want to store the nicknames of players who are on the server. +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`](/docs/std/containers/strings/string/). @@ -37,7 +35,7 @@ of type [`std::string`](/docs/std/containers/strings/string/). <><Code_Motivation/></> 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 use "copy -> paste" a couple additional times. Repeating this is a bad idea. +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? @@ -49,7 +47,7 @@ Instead, we will introduce an array. /> <br/> -An array is a container that can store *some* number of elements of a certain type. +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**. @@ -73,11 +71,18 @@ Unsafe ❌: - a dynamic C-style array (e.g `int* array = new int[5]`) - a fixed-size C-style array (e.g `int array[5]`) -We will focus on the first option, because it is safe and easy to use. -Then we will talk about the last two, which are not recommended to use, and instead we'll provide alternative solutions. +<details> +<summary>Unsafe and non-standard ❌</summary> +<div> + +- a variable-length C-style array (e.g `int array[UserInput]`) + +</div> +</details> -You'll find a full lesson about `std::array` later in the course, because its main advantage is a tiny performance improvement -over `vector` which is not important for the moment. +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. <details> <summary>Were you told to use C-style arrays?</summary> @@ -88,7 +93,7 @@ we won't discuss here. C++ is a language that is constantly evolving, and the C- 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 -problems that can occur when using them, and is able to avoid them, can use them safely. However, this is not the case +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. @@ -96,19 +101,21 @@ there is no situation where not using C-style array would stand in your way. </div> </details> -### Dynamic vs fixed-size arrays +### Fixed-size vs dynamic arrays -A **dynamic array** can grow and shrink as needed. In C++ we use [`std::vector`](/docs/std/containers/arrays/vector/) for this purpose. - -A **fixed-size array** on the other hand has a constant number of elements, which has to be known at compile time. +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`](/docs/std/containers/arrays/array/) is used for this purpose. -Because of the limitation of the fixed-size array we mentioned above, we'll now focus on the dynamic array because it is easier to use. +A **dynamic array** on the other hand can grow and shrink as needed. In C++ we use [`std::vector`](/docs/std/containers/arrays/vector/) +for this purpose. + +The limitations of fixed-size arrays we mentioned above make it harder to use than dynamic arrays. +In the following lessons we'll focus on the `vector` because of that. ### Common operations -There are a lot of things you can do with an array. We'll now go through the most common operations that will cover most of your needs: +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