Practice
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Frontend UI Machine Coding
Resources
Career Advice and Roadmaps
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Backend Development
Frontend Development
Project Ideas for Software Developers
Core Computer Science
Companies
SDE Jobs & Internships
Interview Questions
Compare Companies
IDE
Online IDE
Collaborative IDE

C++ STL: arrays (Complete Guide)

Divya Jain
Divya Jain

Arrays in STL are sequential containers that are very similar to static arrays. They are continuous blocks of memory and have static sizes. Although there are very similar to static arrays in C++, their member functions make them more efficient. They are implemented as static arrays in memory. The array is present in #include<array> header file.

Array Declaration

Syntax:

array<data_type,size> name;

data_type: The data types of the elements to be stored in the array

size: The size of the array

Example:

array < int, 5 > arr; //initializes a array of size 5 which stores integer values 

Array Initialization

Syntax:

array<data_type,size> name{initial_values};

initial_values: optional parameter which initializes the array with the given values.

Example:

array < int, 5 > arr { 1, 2, 3, 4, 5 }; //initializes a array of size 5 having intial values 1,2,3,4,5

Functions on arrays

  • [i]: It is used to access the element stored at index i. 
    Time Complexity: O(1) 
    Parameters: index of the element
    Return type: element at index i
     
  • at(i): It is also used to access the element stored at index i. 
    Time Complexity: O(1) 
    Parameters: index of the element
    Return type: element at index i if i in range of array otherwise out_of_bound error is thrown
     
  • front(): It returns the first element of the array.  
    Parameters: None 
    Return type: the first element of the array
     
  • back(): It returns the last element of the array. 
    Parameters: None 
    Return type: last element of the array.
     
  • size(): It tells us the number of elements in the array. 
    Parameters: None 
    Return type: integer - elements in the vector
     
  • fill(element): It inserts the given value at every array index. 
    Parameters: value to be inserted. 
    Return type: void
     
  • front(): It returns an iterator pointing to the first element of the array. 
    Parameters: None 
    Return type: iterator
     
  • back(): It returns an iterator pointing to the last element of the array. 
    Parameters: None 
    Return type: iterator
     
  • empty(): It tells us whether the array is empty or not. 
    Parameters: None 
    Return type: Boolean, true if the array is empty else false
     
  • data(): It returns a pointer pointing to the first element of the array. 
    Parameters: None 
    Return type: iterator, pointing to the first element of the array
     
  • swap(array_name): It swaps the content of the two arrays.
    Parameters: array_name to be swapped with 
    Return type: void
#include<iostream>
#include<array>
using namespace std;

int main() {
  array < int, 5 > arr { 1, 2, 3, 4, 5 };
  for (int i = 0; i < 5; i++) {
    cout << arr[i] << " ";
  }
  cout << '\n';
  cout << "Size of array is " << arr.size() << '\n';
  cout << "First element of array is " << arr.front() << '\n';
  cout << "Last element of array is " << arr.back() << '\n';
  cout << "Second element of array is " << arr.at(1) << '\n';
  cout << "First element of array using data method is " << * (arr.data()) << '\n';
  arr.fill(10);
  cout << "The new array is ";
  for (int i = 0; i < 5; i++) {
    cout << arr[i] << " ";
  }
  cout << '\n';
  if (arr.empty() == false) {
    cout << "array is not empty!";
  }
  return 0;
}

Output

1 2 3 4 5 
Size of array is 5
First element of array is 1
Last element of array is 5
Second element of array is 2
First element of array using data method is 1
The new array is 10 10 10 10 10 
array is not empty!
Divya Jain
Divya Jain
Divya is an incoming SDE at Atlassian. She loves solving problems and web development. She loves to explore new things and is up for interesting conversations about tech.
SDE Bootcamp - Become a software engineer at a product-based company
Practice Data Structures & Algorithms
Learning Resources
Interview Prep Resources
Blog
  • Career Advice and Roadmaps
  • Data Structures & Algorithms
  • Machine Coding Round (LLD)
  • System Design & Architecture
  • Backend Development
  • Frontend Development
  • Awesome Project Ideas
  • Core Computer Science
Practice Questions
  • Machine Coding (LLD) Questions
  • System Design (HLD) Questions
  • Topic-wise DSA Questions
  • Company-wise DSA Questions
  • DSA Sheets (Curated Lists)
  • JavaScript Interview Questions
  • Frontend UI Machine Coding Questions
Online Compilers (IDE)
  • Online Java Compiler
  • Online C++ Compiler
  • Online C Compiler
  • Online Python Compiler
  • Online JavaScript Compiler
Topic-wise Problems
  • Dynamic Programming Interview Questions
  • Linked List Interview Questions
  • Graph Interview Questions
  • Backtracking Interview Questions
  • Arrays Interview Questions
  • Trees Interview Questions
Company-wise Problems
  • Amazon Interview Questions
  • Microsoft Interview Questions
  • Google Interview Questions
  • Flipkart Interview Questions
  • Adobe Interview Questions
  • Facebook Interview Questions
DSA Sheets (Curated Lists)
  • Top Interview Questions
  • FAANG Interview Questions
  • Most Asked Interview Questions
  • 6 month DSA Practice Sheet
  • 3 month DSA Practice Sheet
  • Last minute DSA Practice Sheet