Array Containers in C++

 Array Containers 


Introduction

·       A fixed-size sequence container that stores elements contiguously in memory.

·       Use STL (standard template library),

·       Similar to built-in arrays but with added safety and utility functions.

·       It is defined in the header file:

#include <array>

STL in c++ : Everything you Need to Know - Edureka


Key Features

Feature

Description

Static size

Size must be known at compile-time (cannot be resized).

Fast access

Elements are stored contiguously → supports random access.

STL compatible

Works with STL algorithms (like sort(), find(), etc.).

Type safety

Prevents accidental array decay to pointer (unlike raw arrays).

Member functions

Provides functions like .at(), .size(), .fill(), etc.

 

Syntax

Declare first header file:- #include <array>

 

array<data_type, size> array_name;

 

 Example:

#include <iostream>

#include <array>

using namespace std;

 

int main() {

    array<int, 5> arr = {10, 20, 30, 40, 50};

 

    cout << "Elements: ";

    for (int i = 0; i < arr.size(); i++)

        cout << arr[i] << " ";

}

 

Output:

Elements: 10 20 30 40 50


Important Member Functions

Function

Description

Example

arr.at(index)

Access element with bounds checking

arr.at(2)

arr[index]

Access element (no bounds check)

arr[2]

arr.front()

Returns the first element

arr.front()

arr.back()

Returns the last element

arr.back()

arr.size()

Returns the total number of elements

arr.size()

arr.fill(value)

Fills the array with the given value

arr.fill(0)

arr.swap(other)

Swaps content with another array of the same type

arr1.swap(arr2)

arr.begin(), arr.end()

Iterators for traversal

Used in loops or algorithms


Example: Demonstrating All Functions

#include <iostream>

#include <array>

#include <algorithm> // for sort()

 

using namespace std;

 

int main() {

    array<int, 5> arr = {5, 2, 8, 1, 3};

 

    cout << "Original array: ";

    for (int x : arr) cout << x << " ";

    cout << endl;

 

    // Accessing elements

    cout << "First: " << arr.front() << endl;

    cout << "Last: " << arr.back() << endl;

    cout << "At(2): " << arr.at(2) << endl;

 

    // Sorting

    sort(arr.begin(), arr.end());

    cout << "Sorted array: ";

    for (int x : arr) cout << x << " ";

    cout << endl;

 

    // Fill with value

    arr.fill(9);

    cout << "After fill: ";

    for (int x : arr) cout << x << " ";

    cout << endl;

 

    return 0;

}

 

Output:

Original array: 5 2 8 1 3

First: 5

Last: 3

At(2): 8

Sorted array: 1 2 3 5 8

After fill: 9 9 9 9 9


Limitations

  • Size must be known at compile time.
  • Cannot resize dynamically.
  • For dynamic arrays → use std::vector.

Use Case

  • need fixed-size arrays with STL features.
  • Performance and contiguous memory access are important (like in embedded systems or numerical computations).

 

Types of Array Containers

Container

Header

Size Known At

Key Features

std::array

<array>

Compile time

Fixed size, STL-compatible

std::vector

<vector>

Runtime

Dynamic size, resizable

std::valarray

<valarray>

Runtime

Numerical computing

std::dynarray

(Deprecated)

Removed in C++14

Article Reference:-  web resources 

====================================================================  

Post a Comment

0 Comments