# std::array Tutorial and Examples in C++

The array is a container for arrays of constant size. This container wraps around fixed-size arrays and does not lose length information when decayed to a pointer.
To use array, we must include the array header.

 #include <array>

## Array Tutorial and Examples

### 1)Creating and initializing std::array objects

i)

std::array<int,5> arr;

In this case, the std::array object arr represents an int array of fixed size 5 that is uninitialized, so all ten elements have garbage values.

ii)

std::array<std::string, 10000> array1;

Here, std::array1 array object represents a fixed-size string array of 10000 characters.

iii)

// First 3 values will be initialized and others will be 0.
std::array<int, 8> array1 = { 78, 33 ,66 };

If we have fewer elements during initialization, the remaining elements will be set to default values. In the case of int, it is 0.

Output:

78 , 33 , 66 , 0 , 0 , 0 , 0 , 0

iv)

std::array also has a member function fill() that allows you to set the same value for all elements at once. It is simple when we have a large array.

std::array<int, 10> array1;
// Filling all elements with the value 8
array1.fill(8);

Output:

8 , 8 , 8 , 8 , 8 , 8 , 8 , 8

Below is the implementation of all above methods:

#include <bits/stdc++.h>
using namespace std;

void displayArray(std::array<int, 8>& array1)
{
// displaying the array by iterating over the array
for (auto& element : array1)
cout << element << " ";
cout << endl;
}
int main()
{
// Taking a array and initiazing with garbage values
array<int, 8> array1;
displayArray(array1);
// initializing the array with some values
array<int, 8> array2
= { 10, 20, 30, 40, 50, 60, 70, 80 };
displayArray(array2);
// Only first three values are initialized and remaining
// values are initialized with 0
array<int, 8> array3 = { 23, 46, 28 };
displayArray(array3);
array<int, 8> array4;
// filling all the values with the value 8
array4.fill(8);
displayArray(array4);

return 0;
}

Output:

-184795504 32765 1 0 -184795504 32765 6299128 0
10 20 30 40 50 60 70 80
23 46 28 0 0 0 0 0
8 8 8 8 8 8 8 8

### 2)Printing size of array

We can get size of array using size() function

Syntax:

array_name.size()

#include <bits/stdc++.h>
using namespace std;

int main()
{
// initalizing the array with some values
array<int, 8> array1 = { 8, 3, 4, 5, 6, 7, 1, 4 };
cout << "Size of array = " << array1.size();

return 0;
}

Output:

Size of array = 8

### 3)Accessing the elements in the array

Method #1:Using [] operator

We can access the elements in the array using [] operator.

int x = array1[5];

Using the [] operator to access elements that are out of range will result in undefined behaviour.

Method #2:Using at() function

We can access the elements in the array using at() function.

int x = array1.at(5);

Using the at() function to access an out-of-range element will result in an out of range exception.

Method #3:Using get<>() function

We can access the elements in the array using get<>() function.

int x = std::get<5>(arr);

Using the get<> operator to access elements that are out of range will result in a compile time error.

### 4)Traversing over the array object

In the following example, we will look at four different ways to iterate over an array.

• using Range based for loop
• usingfor loop
• using iterator
• Using for_each

Below is the implementation:

#include <bits/stdc++.h>
using namespace std;

int main()
{
// intializing
// initalizing the array with some values
array<int, 8> array1 = { 8, 3, 4, 5, 6, 7, 1, 4 };
// using range based for loop
cout << "Using range based for loop" << endl;
for (auto& element : array1) {
cout << element << " ";
}
cout << endl;
// using for loop
cout << "Using for loop" << endl;
for (int index = 0; index < array1.size(); index++) {
cout << array1[index] << " ";
}
cout << endl;
cout << "Using iterator" << endl;
// initializing a iterator which points to first element
// of the array
auto itr = array1.begin();
while (itr != array1.end()) {
cout << *itr << " ";
itr++;
}
cout << endl;
// using for_each
cout << "using for_each" << endl;
for_each(array1.begin(), array1.end(),[](const int& element)
{
cout << element << " ";
});
cout << endl;

return 0;
}

Output:

Using range based for loop
8 3 4 5 6 7 1 4
Using for loop
8 3 4 5 6 7 1 4
Using iterator
8 3 4 5 6 7 1 4
using for_each
8 3 4 5 6 7 1 4

Related Programs: