The array is a container for constant size arrays. This container wraps around fixed size arrays and also doesn’t loose the information of its length when decayed to a pointer.


array<object_type, array_size> array_name;

The above code creates an empty array of object_type with maximum size of array_size.

Member Functions

  • reference back();
    const_reference back() const;
    Returns a reference to the last element in the array container.
  • iterator begin() noexcept;
    const_iterator begin() const noexcept;
    Returns an iterator pointing to the first element in the array container.
  • reference front();
    const_reference front() const;
    Returns a reference to the first element in the array container.
  • void swap (array& x) noexcept(noexcept(swap(declval<value_type&>(),declval<value_type&>())));
    Exchanges the content of the array by the content of x, which is another array object of the same type (including the same size).
  • constexpr size_type size() noexcept;
    Returns the number of elements in the array container.



// Program to demonstrate working of array 
#include <algorithm> 
#include <array> 
#include <iostream> 
#include <iterator> 
#include <string> 
using namespace std; 

int main() { 

  // Construction uses aggregate initialization
  array<int, 5> ar1{{3, 4, 5, 1, 2}}; 
  array<int, 5> ar2 = {1, 2, 3, 4, 5}; 
  array<string, 2> ar3 = {"a", "b"}; 

  // Sizes of array
  cout << ar1.size() << endl; 
  // Content of array
  for (auto i : ar1) 
    cout << i << ' '; 

  // Container operations are supported 
  sort(ar1.begin(), ar1.end()); 

  // Filling ar2 with 10 

  // Value at index 2
  cout <<     
  // Array content using Iterator
  for (auto it = ar3.begin(); it != ar3.end(); ++it )
    cout <<*it;
  // Swap array content  
  ar1.swap (ar2);
  ar2.back() = 50;
  return 0;