C++

How to Use std::array

The std::array notation in C++ is an alternate method of declaring and initializing the arrays in C++. However, a question might arise in your mind that when we can declare and initialize the arrays already, then why do we even need this notation in the first place? In this article, we will try to explore the answer to this question. After that, we will share some examples with you to demonstrate the usage of the std::array notation in C++ in Ubuntu 20.04.

The std::array Notation in C++:

We have already stated that the std::array notation in C++ is an alternate method of declaring and initializing the arrays. However, we still need to figure out the need to use this notation. Generally, there are two types of arrays in C++, i.e., static and dynamic. Both these types of arrays have their pros and cons. The static arrays lose all the information regarding their size once they are passed to a function as a pointer, whereas the dynamic arrays’ deallocation is very problematic.

Therefore, the std::array notation is used to make the best usage of both of these types, i.e., using this particular notation, a static array never loses the information that it contains even when it is passed to a function. This is exactly why we use this notation in C++. You will learn more about the usage of this notation in C++ by going through the next section of this article.

Using std:array in C++ in Ubuntu 20.04:

To understand the usage of the std::array notation in C++ in Ubuntu 20.04, you will have to take a look at the following examples:

Example # 1: Using an Initializer List to Declare an Array:

In this example, we intend to teach you to use an initializer list for initializing an array using the std::array notation in C++. We have written the C++ code shown in the image below to demonstrate this:

In this code, we have also included the “<array>” library along with the regular “<iostream>” library, followed by including the namespace “std”. Then, inside our “main()” function, we have declared and initialized an array using the initializer list notation in C++. In this notation, an array is declared by stating the “std::array” keyword followed by the data type and size of the array enclosed within angle brackets. Then, we have to mention the name of the array, which in our case is “testArray”. After doing that, the array is initialized by putting an “=” symbol followed by the elements of the array enclosed within braces.

Once the array is declared using the initializer list in C++, we have a “for” loop that iterates through a variable “i” that goes to the array size and increments after every iteration. Then, within this “for” loop, we simply have a “cout” statement that is there to print the elements of the array on the terminal. Finally, the code wraps up with the “return 0” statement.

We compiled our C++ code file stdArray.cpp with the following command:

$ g++ stdArray.cpp –o stdArray

Then, we executed our object file using the command stated below:

$ ./stdArray

All the elements of our array are shown in the following image:

Example # 2: Using the List Initialization Notation to Declare an Array:

In this example, we want to teach you the usage of the list initialization notation for initializing an array in C++. We have written the C++ code shown in the image below to demonstrate this:

The code shown in the image above is pretty much similar to the one shared in our first example. However, only the method of initializing the array is different. We have used the list initialization notation this time. In this notation, there is no “=” symbol, whereas the rest of the statement remains the same as that of the initializer list notation.

After compiling and executing this modified C++ code, the elements of our array are shown in the following image:

Example # 3: Separately Declaring an Array Followed by its Initialization:

In this example, we will learn yet another method of declaring an array using the std::array notation in C++. In this method, we will separately declare an array followed by its initialization. The C++ code written for this purpose is shown in the image below:

In this code, we have used the statement “std::array<int, 3> testArray” to declare our array, whereas the initialization of the array takes place in the next step using the statement “testArray = {10, 20, 30}”. The rest of the code is the same as that of our first two examples.

The output of this modified C++ code is the same as that of our second example since only the declaration and initialization method was different. The rest of everything was the same. You can see this from the following image:

Example # 4: Accessing a Specific Index of the Array:

Now, we will take you a step further. By now, you would have clearly understood the three different methods of declaring and initializing an array using the std::array notation in C++ in Ubuntu 20.04. Now, we will tell you how you can access a specific index of an array. For showing you that, we have written the C++ code shown in the image below:

In this example, we used the notation used in our third example to declare and initialize our array. After that, we wanted to access the second index of our array, i.e., testArray[1]. For that, we have simply used the same notation as we have just started with our “cout” statement within our code.

When we compiled and executed our C++ code, the second element of our array was displayed on the terminal, as shown in the following image:

Example # 5: Changing the Value Assigned to a Specific Index of an Array:

In this example, we want to explain to you the method of changing the value of a specific index of an array that has already been assigned to it. For that, you will have to see the code shown in the image below:

In this example, we have declared and initialized our array using the same notation we used in our third example. After that, we wanted to change the value assigned to the second index of the array to “25”. For doing so, we have used the statement “testArray[1] = 25”. Then, we have simply displayed the modified value of the second index of our array on the terminal, followed by the “return 0” statement.

When we compiled and executed this C++ code, the changed value of the second index of our array, i.e., 25, appeared on our terminal as shown in the following image:

Conclusion:

This article revolved around the usage of the std::array notation in C++ on a Ubuntu 20.04 system. We first shared with you the reason behind using this notation in C++. After making this reason clear, we implemented five different examples in C++ to show you the usage of the std::array notation. With the help of these examples, you can conveniently declare, initialize, display, modify, etc., the arrays using the std::array notation in C++ in Ubuntu 20.04.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.