1. C++ std::array Introduction
#include<array>
2. what are the advantages of std::array over C style array?
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
3.2 One Dimensional array initialization:
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
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
4. Passing std::array to 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;
}
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.
#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;
}
Printing elements by using [] operator
1 3 2 4 5
Printing elements by using at operator
1 3 2 4 5
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;
}
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.
#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