Signup/Sign In

ARRAY Container in STL

Arrays, as we all know, are collection of homogenous objects. array container in STL provides us the implementation of static array, though it is rarely used in competitive programming as its static in nature but we'll still discuss array container cause it provides some member functions and non-member functions which gives it an edge over the array defined classically like, int array_name[array_size].

SYNTAX of array container:

array<object_type, array_size> array_name;

The above code creates an empty array of object_type with maximum size of array_size. However, if you want to create an array with elements in it, you can do so by simply using the = operator, here is an example :

#include <vector>

int main()
{
    array<int, 4> odd_numbers = { 2, 4, 6, 8 };
}

The above statement will create an array with 2,4,6,8 as data in the array. Note that initialization with {} brackets is only possible in c++ 17.


Member Functions of Array Template

Following are the important and most used member functions of array template.


at function

This method returns value in the array at the given range. If the given range is greater than the array size, out_of_range exception is thrown. Here is a code snippet explaining the use of this operator :

#include <iostream>
#include <array>

using namespace std;

int main ()
{
    array<int,10> array1 = {1,2,3,4,5,6,7,8,9};
   
    cout << array1.at(2)     // prints 3
    cout << array1.at(4)     // prints 5
  
}

[ ] Operator

The use of operator [ ] is same as it was for normal arrays. It returns the value at the given position in the array. Example : In the above code, statement cout << array1[5]; would print 6 on console as 6 has index 5 in array1.


front() function

This method returns the first element in the array.


back() function

This method returns the last element in the array. The point to note here is that if the array is not completely filled, back() will return the rightmost element in the array.


fill() function

This method assigns the given value to every element of the array, example :


#include <array>
int main()
{
    array<int,8> myarray;
    myarray.fill(1);
}

This will fill the array myarray with value as 1, at all of its 8 available positions.


swap function

This method swaps the content of two arrays of same type and same size. It swaps index wise, thus element of index i of first array will be swapped with the element of index i of the second array, and if swapping any of the two elements thows an execption, swap() throws exception. Below is an example to demonstrate its usage :


#include <array>

int main()
{
    array<int,8> a = {1,2,3,4,5,6,7,8};
    array<int,8> b = {8,7,6,5,4,3,2,1};
    
    a.swap(b)  // swaps array a and b
    
    cout << "a is : ";
    for(int i=0; i < 8; i++) {
    cout << a[i] <<" ";
    }
    cout << endl;
    cout << "b is : ";
    for(int i=0; i < 8; i++) {
    cout << a[i] <<" ";
    }
    /* ouput will be 
    a is : 8 7 6 5 4 3 2 1
    b is : 1 2 3 4 5 6 7 8 */
}

operators ( == , != , > , < , >= , <= )

All these operators can be used to lexicographically compare values of two arrays.


empty function

This method can be used to check whether the array is empty or not.

Syntax: array_name.empty(), returns true if array is empty else return false.


size function

This method returns the number of element present in the array.


max_size function

This method returns the maximum size of the array.


begin function

This method returns the iterator pointing to the first element of the array. Iterators are just like pointers and we’ll discuss them later in the lessons, for now you can just think of an iterator like a pointer to the array.

Begin and End in array

end function

This method returns an iterator pointing to an element next to the last element in the array, for example the above array has 4 elements and the end() call will return the iterator pointing to the 4th index of the array.