Role Of Arrays In STL With Examples.
Arrays are contiguous memory locations. Array container is a sequential homogenous container and is of a fixed size.
Actually, in programming, we rarely use such a static container as in real-time scenarios we need containers that can expand or shrink dynamically. Nevertheless, as an array is one of the basic containers, we will begin our discussions about STL containers with arrays.
=> Check ALL C++ Tutorials Here.
In this tutorial, we will discuss arrays in general along with the various methods that can be used with array containers and the example to use arrays and its methods in a program.
Table of Contents:
Arrays In STL
The general syntax of declaring an array container is:
array<object_type, size> array_name;
The above declaration creates an array container ‘array_name’ with size ‘size’ and with objects of type ‘object_type’.
We can also initialize this array container as shown below,
Array<int,5> myarray = {1,1,2,3,5};
The above array container can be represented pictorially as:
The header we need to include for array container is <array>.
Array container supports various operations that can be carried out to facilitate efficient traversing and manipulation of array container elements.
Some of the functions that are supported by the array container include:
- At: Returns value in the array container at a given position. ‘Out_of_range’ exception is thrown if the position specified is beyond the array limits.
- Front: Returns the first element in the array container.
- Back: Returns the last element in the array container if the container is completely filled the other returns the rightmost element in the container.
- Fill: Assigns a given value to every element in the array container.
- Swap: Swaps contents of two arrays with the same type and same size index wise.
- Empty: Boolean function to check if an array container is empty or not.
- Size: Returns the number of elements in the array container.
- Max_size: Returns the maximum size of the array container.
- Begin: Returns the iterator pointing to the beginning of the array container i.e. first element of the array.
- End: Returns the iterator pointing to the location next to the last element in the array container.
Given below is a pictorial representation of an array of odd numbers with size 5:
As shown above, begin() function of array container returns the value pointed at the beginning of the array i.e. first element of the array.
End() function returns the value pointed at the end of the array. This may not necessarily be the last element in the array. As shown above, though the size of the array is 5, only 4 elements are present.
Apart from the above operations, the array container also supports the following operators:
(i) []: This operator is similar to the ‘at’ operation and can be used in the same way that is used for simple arrays.
(ii) Operators – == , != , > , < , >= , <= : All these operators are used to compare the array container values lexicographically.
Additionally, we can also use the already discussed algorithms like sort, reverse, etc., with array containers.
Next, we will demonstrate the use of operations described above on the array container using a program.
#include <algorithm> #include <array> #include <iostream> #include <iterator> using namespace std; int main() { array<int, 5> myarray = {1, 1, 2, 3, 5}; cout << "Size of array: " << endl; cout << myarray.size() << endl; cout << "\nmyarray contents: "; for (auto i : myarray) cout << i << ' '; // sort operation sort(myarray.begin(), myarray.end()); cout << "\nsorted myarray : "; for (auto i : myarray) cout << i << ' '; cout<<"\nFirst element of myarray "<<myarray.at(0); cout<<endl; cout<<"FRONT myarray: "<<myarray.front(); cout<<endl; cout<<"BACK myarray: "<<myarray.back(); cout<<endl; // Filling ar2 with 10 myarray.fill(8); cout << "\nFilled myarray : "; for (auto i : myarray) cout << i << ' '; return 0; }
Output:
Size of array:
5
myarray contents: 1 1 2 3 5
sorted myarray: 1 1 2 3 5
The first element of myarray 1
FRONT myarray: 1
BACK myarray: 5
Filled myarray: 8 8 8 8 8
The above program shows the various operations supported by the array container.
Conclusion
We have seen the sort function being used on array container. All the functions are self-explanatory and demonstrate how these operations can be used to efficiently program the array container.
In our upcoming tutorial, we will explore all about vector container of STL.
=> Visit Here To See The C++ Training Series For All.