CPP STL Tutorial : std::array and it’s operations

Note: std::array feature is only available in C++11 and above.
In this chapter we shall learn about below topics:,
1.  array introduction.
2.  array declaration.
3.  Multidimensional std::array
4.  Passing std::array to function
5.  array member function to iterate over elements.
6.  array member function to check the capacity.
7.  array member function to access the elements.
8.  array member function to modify the elements.

1. C++ std::array Introduction

Array is a fixed storage/container that will store data of same type.
In order to use array, you need to include below header file:
#include<array>

2. what are the advantages of std::array over C style array?

1. It wont convert into a pointer. Hence array decay will not happen.
2. It has bound checking
3. As it has bound checking, you cannot insert elements greater than the size that you have allocated.

3.  std::array declaration:

3.1 One dimensional array can be declared as below:

std::array<int, 5> int_array; // we declared an array of type int and with length 5
It is important to give the size of the array during compile time. Forgetting to do so, compiler will give an error.

3.2 One Dimensional array initialization:

Type 1:
std::array<int, 5> int_array = {1, 3, 2, 4, 5};
Type 2:
std::array<int, 5> int_array {1, 3, 4, 1, 5};
Type 3:
std::array<int, 5> int_array = {1, 2};// elements at the index 2, 3, 4 are set to zero
Type 4:
std::array<string, 2> str_array = {{string(“a”), “b”}};

3.3 Two dimensional array can be declared as below:

std::array<std::array<int, 5>, 5> int_2D_array; // we declared an array of type int and with length 5
It is important to check the nesting of 2D array declaration.

3.4 Two dimensional array can be initialized as below:

std::array<std::array<int, 3>, 3> int_2D_array = { { {1, 2, 3},
						     {4, 5, 6},
						     {7, 8, 9}
						   }// C array
					         } // std::array class braces
It is important to note the additional braces that encloses the actual array. This is because, std::array is an aggregator to C array. Hence when you declare a simple C array, the whole array needs to be included into the std:array class. Hence the extra set of braces.

4. Passing std::array to function

Below function shows how to pass array arguments to a function:
#include<iostream>
#include<array>

using namespace std;

void display_1D_array(array<int, 5> &int_array)
{
	cout<<"1D array is :"<<endl;
	for(int i = 0; i < 5; i++)
	{
		cout<<int_array[i]<<" ";
	}
	cout<<"\n";
}

void display_2D_array(array< array<int, 3>, 3> &int_array)
{
	cout<<"2D array is :"<<endl;
	for(int i = 0; i < 3; i++)
	{
		for(int j = 0; j < 3; j++)
		{
			cout<<int_array[i][j]<<" ";
		}
		cout<<"\n";
	}
	cout<<"\n";
}

int main()
{
	array<int, 5> int_array = {1, 3, 2, 4, 5};
	display_1D_array(int_array);

	array<array<int, 3>, 3> int_2D_array = {
		{
			{1, 2, 3},
			{4, 5, 6},
			{7, 8, 9}
		}
	};
	display_2D_array(int_2D_array);

	return 0;
}

Output:

1D array is :
1 3 2 4 5
2D array is :
1 2 3
4 5 6
7 8 9

5. array member function to iterate over elements.

begin(): It will return an iterator pointing to the beginning of the array.
end(): It will return an iterator pointing to the end of the array.

They are always used in pairs.

Below is the simple example of begin() and end().

#include<iostream>
#include<array>

using namespace std;

int main()
{
	array<int, 5> int_array = {1, 3, 2, 4, 5};

	cout<<"Printing elements by using begin() and end()"<<endl;

	for(auto i = int_array.begin(); i < int_array.end(); i++)
	{
		cout<<*i<<"   "; // *i is used to access the element pointed by i.
	}
	cout<<"\n";

	return 0;
}

Output:

Printing elements by using begin() and end()
1 3 2 4 5

rbegin(): It will return a reverse iterator pointing to the end of the array.
rend(): It will return a reverse iterator pointing to the starting of the array.

They are always used in pairs.

Below is the simple example of rbegin() and rend().

#include<iostream>
#include<array>

using namespace std;

int main()
{
	array<int, 5> int_array = {1, 3, 2, 4, 5};

	cout<<"Printing elements by using rbegin() and rend()"<<endl;

	for(auto i = int_array.rbegin(); i < int_array.rend(); i++)
	{
		cout<<*i<<"   "; // *i is used to access the element pointed by i.
	}
	cout<<"\n";

	return 0;
}

Output:

Printing elements by using rbegin() and rend()
5 4 2 3 1

6. array member function to check the size of array.

size(): It will return the number of elements in the array.
max_size(): it returns the max size of the array.
Both size() and max_size() will return the same value.

empty(): It is used to check if array is empty or not.

Below is the simple example of size(), max_size() and empty().

#include<iostream>
#include<array>

using namespace std;

int main()
{
	array<int, 10> int_array = {1, 3, 2, 4, 5, 6};
	array<int, 0> my_array;

	cout<<"The size of the array by using size() is = "<< int_array.size()<<endl;
	cout<<"The size of the max size of the array by using max_size() is = "<< int_array.max_size()<<endl;
	cout<<"Checking if the int_array is empty by using empty() is = "<< int_array.empty()<<endl;
	cout<<"Checking if the my_array is empty by using empty() is = "<< my_array.empty()<<endl;

	return 0;
}
Output:
The size of the array by using size() is = 10
The size of the max size of the array by using max_size() is = 10
Checking if the int_array is empty by using empty() is = 0
Checking if the my_array is empty by using empty() is = 1

7. array member function to access the elements.

[] Used to access element at the index provided.
at Used to access element at the index provided.
Below simple program to check usage of above 2 functions.
#include<iostream>
#include<array>

using namespace std;

int main()
{
	array<int, 5> int_array = {1, 3, 2, 4, 5};

	cout<<"Printing elements by using [] operator"<<endl;

	for(int i = 0; i < int_array.size(); i++)
	{
		cout<<int_array[i]<<"   ";
	}
	cout<<"\n";

	cout<<"Printing elements by using at operator"<<endl;

	for(int i = 0; i < int_array.size(); i++)
	{
		cout<<int_array.at(i)<<"   ";
	}
	cout<<"\n";

	return 0;
}
Output:
Printing elements by using [] operator
1   3   2   4   5
Printing elements by using at operator
1   3   2   4   5
front Used to access first element.
back Used to access last element
data Used to get the pointer pointing to the first element.
Below simple program to check usage of above 3 functions.
n#include<iostream>
#include<array>

using namespace std;

int main()
{
	array<int, 5> int_array = {1, 3, 2, 4, 5};
	cout<<"The first element of the array using front() is "<< int_array.front()<<endl;
	cout<<"The last element of the array using back() is "<< int_array.back()<<endl;
	cout<<"The first element of the array using data() is "<< *int_array.data()<<endl;
	return 0;
}
Output:
The first element of the array using front() is 1
The last element of the array using back() is 5
The first element of the array using data() is 1

8. array member function to modify the elements.

fill This function is used to set all the elements of the array with the value provided.
swap This function is used to swap 2 arrays, whose size and data type should be same.
Below simple program to check usage of above 2 functions.
#include<iostream>
#include<array>

using namespace std;

int main()
{
	array<int, 5> int_array;
	int_array.fill(3);

	cout<<"Printing elements, elements filled by fill()"<<endl;

	for(int i = 0; i < int_array.size(); i++)
	{
		cout<<int_array.at(i)<<"   ";
	}
	cout<<"\n";

	array<int, 5> int_array_1 = {1, 2, 3, 4, 5};
	array<int, 5> int_array_2 = {6, 7, 8, 9, 10};

	cout<<"\nPrinting array_1 before swap"<<endl;

	for(int i = 0; i < int_array_1.size(); i++)
	{
		cout<<int_array_1.at(i)<<"   ";
	}

	cout<<"\nPrinting array_2 before swap"<<endl;

	for(int i = 0; i < int_array_2.size(); i++)
	{
		cout<<int_array_2.at(i)<<"   ";
	}

	//performing swap operation
	int_array_1.swap(int_array_2);

	cout<<"\nPrinting array_1 after swap"<<endl;

	for(int i = 0; i < int_array_1.size(); i++)
	{
		cout<<int_array_1.at(i)<<"   ";
	}

	cout<<"\nPrinting array_2 after swap"<<endl;

	for(int i = 0; i < int_array_2.size(); i++)
	{
		cout<<int_array_2.at(i)<<"   ";
	}

	return 0;
}

Output:

Printing elements, elements filled by fill()
3 3 3 3 3

Printing array_1 before swap
1 2 3 4 5
Printing array_2 before swap
6 7 8 9 10
Printing array_1 after swap
6 7 8 9 10
Printing array_2 after swap
1 2 3 4 5
Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *