C++ STL Array Container – std::array

Here you will learn about STL Array Container in C++ i.e. std::array.

I Hope you know about C-type arrays (arrays in C language). Since C++ is just extension to C language we can use those. The main property is that, Array contains series of elements of same type placed in contiguous memory locations. i.e suppose we have marks of 100 students, instead of declaring 100 variables, using array the 100 int values can store in contiguous memory locations and all can be accessed through same identifier, by just adding some number to identifier which points to particular index.

Declaring Normal Array

Syntax: type arrayName[size];

Example: int marks[5];  //This is array of type int which stores marks of 5 students

Initializing Arrays

int marks [5] = { 10, 23, 15, 20, 25 };

This stores the elements in contiguous locations, that in index 0, element 10 will be stored remaining follows as same.

Accessing the elements of the array:

Syntax: arrayName[index];

Example: marks[2]; //This will gives the value of the element at 2nd index of the array marks, which is 15.

If we try to access giving a number which is greater than size of the array in place of index, it will raise an error called arrayIndexOutof bound error.

These are directly implemented as a language feature, inherited form C language. They probably suffer from an excess of optimization. Very less inbuilt functions.

C++ STL Array

To overcome issues with language built-in arrays, C++ Standard library (STL) provides arrays as a type of container which includes rich set of operations. It is a type template (a class template) defined in header <array>.

To work with that array we must include array header class in our code.

#include<array> // Including array container header file

These offer a better alternative for C-type arrays. The advantages over C-type arrays are:

  • C-style arrays don’t know its own size. But Array Containers know their own size. So when passing array to a function here we no need to pass it’s size unlike where we do in C-type arrays.
  • C-style arrays worked and entirely accessed by pointers, where Array Containers are not.
  • Array Containers have more inbuilt functions than C-style arrays. So these are more efficient.

Syntax of STL Array

array< datatype, size > arrayName;

Operations on Array Container

Lets discuss about some important functions of stl array with program examples.

at(): This method used to access the elements of the array.

Syntax: arrayName.at(index);

In the place of index, if we give number more than the size of the array it will give array out_out_range exception.

get(): This is also used to access the elements of the array. But this method doesn’t belongs to the class array. It is over loaded from class tuple.

Syntax: get<index> (arrayName);

[] operator: This is also used to access the elements of the array. This style accessing is same as C-style arrays.

Example program to show about these three operations on array:

Output

elements using at()
87
98
70
90
100
elements using [] operator
87
98
70
90
100
elements using get()
87
98
70
90
100

front(): This returns the first element of the array.

back(): This returns the last element of the array.

Example program to show front() and back() operations:

Output

first element of the array is 87
last element of the array is 100

size(): It returns the number of elements in the array. C-style arrays fails in this.

max_size(): It returns the maximum number of elements array can hold i.e. the size with which array is declared. The size() and max_size() return the same value.

empty(): If array size is zero this function returns true (1). Else returns false value

fill(): This function used to fill the entire array with particular value.

Example program to demonstrate above four functions:

Output

the number of elements in the array is 5
maximum number of elements in the array is 5
Array is empty
35 35 35 35 35

swap(): This will swap the all the elements of one array1 to array2 and from array2 elements to array1. But condition is that both arrays must be of same type and must be same size.  Swapping will done in such a way that, each ith indices of of both arrays will be swapped.

Example program:

Output

array1 elements before swap
1 2 3 4 5
array2 elements before swap
11 22 33 44 55
array1 elements after swap
11 22 33 44 55
array2 elements after swap
1 2 3 4 5

Comment below if you have any queries or found any information incorrect in above tutorial for C++ STL Array.

3 thoughts on “C++ STL Array Container – std::array”

  1. Shivansh Mathur

    Than why and what is the main difference and use of get function and [] operator for accessing array element?

    1. J V Phani Kumar

      Get(), at(),…. All these functions raise an exception called, out of range exception when we try to access out of range element (i.e index >size)

      But…
      [] operator gives undefined result instead of raising exception when accessing out of range elements

  2. #error This file requires compiler and library support for the ISO C++ 2011 standard. This support must be enabled with the -std=c++11 or -std=gnu++11 compiler options.
    #error This file requires compiler and library support \
    ^
    array1.cpp: In function ‘int main()’:
    array1.cpp:7:5: error: ‘array’ was not declared in this scope
    array marks = {87, 98, 70, 90, 100};
    can someone tell me how to debug the 2nd program(ex: frnt n back operations)

Leave a Comment

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