codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
⚡

C++ Programming

63 / 87 topics
62Introduction to STL & Containers63std::array64Vectors65List & Forward List66Deque67Stack68Queue & Priority Queue69Map & Multimap70Set & Multiset71Unordered Map & Unordered Multimap72Unordered Set & Unordered Multiset73Iterators74Algorithms75Functors
Tutorials/C++ Programming/std::array
⚡C++ Programming

std::array

Updated 2026-04-20
2 min read

Introduction to std::array

std::array is a container that encapsulates fixed-size arrays. It provides a standard interface for accessing and manipulating elements, making it a versatile tool in the C++ Standard Library (STL). Unlike traditional C-style arrays, std::array offers type safety, bounds checking, and compatibility with STL algorithms.

Key Features of std::array

  • Fixed Size: The size of the array is determined at compile time.
  • Type Safety: Elements are of a single type, ensuring type consistency.
  • Bounds Checking: Methods like at() provide safe access to elements with bounds checking.
  • STL Compatibility: Can be used with STL algorithms and functions.

Basic Usage

Declaration and Initialization

#include <array>
#include <iostream>

int main() {
    // Declare a std::array of 5 integers, initialized to zero
    std::array<int, 5> arr1;

    // Initialize with specific values
    std::array<int, 3> arr2 = {1, 2, 3};

    // Using the array constructor
    std::array<double, 4> arr3{4.0, 5.5, 6.7, 8.9};

    return 0;
}

Accessing Elements

#include <iostream>
#include <array>

int main() {
    std::array<int, 5> arr = {10, 20, 30, 40, 50};

    // Using subscript operator []
    std::cout << "Element at index 2: " << arr[2] << std::endl;

    // Using at() method with bounds checking
    try {
        std::cout << "Element at index 10: " << arr.at(10) << std::endl;
    } catch (const std::out_of_range& e) {
        std::cerr << "Error: " << e.what() << std::endl;
    }

    return 0;
}

Iterating Over Elements

#include <iostream>
#include <array>

int main() {
    std::array<int, 5> arr = {10, 20, 30, 40, 50};

    // Using range-based for loop
    for (const auto& elem : arr) {
        std::cout << elem << " ";
    }
    std::cout << std::endl;

    // Using iterators
    for (auto it = arr.begin(); it != arr.end(); ++it) {
        std::cout << *it << " ";
    }
    std::cout << std::endl;

    return 0;
}

Methods and Properties

Size and Capacity

#include <iostream>
#include <array>

int main() {
    std::array<int, 5> arr = {10, 20, 30, 40, 50};

    // Get the size of the array
    std::cout << "Size: " << arr.size() << std::endl;

    // Check if the array is empty (always false for non-zero-sized arrays)
    std::cout << "Empty: " << arr.empty() << std::endl;

    return 0;
}

Element Access

#include <iostream>
#include <array>

int main() {
    std::array<int, 5> arr = {10, 20, 30, 40, 50};

    // Front element
    std::cout << "Front: " << arr.front() << std::endl;

    // Back element
    std::cout << "Back: " << arr.back() << std::endl;

    return 0;
}

Modifying Elements

#include <iostream>
#include <array>

int main() {
    std::array<int, 5> arr = {10, 20, 30, 40, 50};

    // Modify elements using subscript operator []
    arr[2] = 35;

    // Modify elements using at() method
    arr.at(3) = 45;

    for (const auto& elem : arr) {
        std::cout << elem << " ";
    }
    std::cout << std::endl;

    return 0;
}

Filling the Array

#include <iostream>
#include <array>

int main() {
    std::array<int, 5> arr;

    // Fill all elements with a specific value
    arr.fill(100);

    for (const auto& elem : arr) {
        std::cout << elem << " ";
    }
    std::cout << std::endl;

    return 0;
}

Comparison and Sorting

Comparing Arrays

#include <iostream>
#include <array>

int main() {
    std::array<int, 3> arr1 = {1, 2, 3};
    std::array<int, 3> arr2 = {4, 5, 6};

    // Lexicographical comparison
    if (arr1 < arr2) {
        std::cout << "arr1 is less than arr2" << std::endl;
    } else {
        std::cout << "arr1 is not less than arr2" << std::endl;
    }

    return 0;
}

Sorting Arrays

#include <iostream>
#include <array>
#include <algorithm>

int main() {
    std::array<int, 5> arr = {34, 7, 23, 32, 5};

    // Sort the array in ascending order
    std::sort(arr.begin(), arr.end());

    for (const auto& elem : arr) {
        std::cout << elem << " ";
    }
    std::cout << std::endl;

    return 0;
}

Best Practices

  1. Use std::array Over C-style Arrays: std::array provides better type safety and compatibility with STL algorithms.
  2. Initialize Properly: Always initialize std::array to avoid undefined behavior.
  3. Leverage Bounds Checking: Use at() for safer element access, especially in production code.
  4. Use Range-based For Loops: They are concise and readable for iterating over elements.

Conclusion

std::array is a powerful and flexible container that should be part of every C++ developer's toolkit. Its fixed size, type safety, and compatibility with STL algorithms make it an excellent choice for scenarios where you need a simple, efficient array-like structure. By following best practices and leveraging its features, you can write safer, more maintainable code.

Additional Resources

  • C++ Reference: std::array
  • Effective Modern C++ by Scott Meyers

This comprehensive guide provides a detailed overview of std::array, covering its declaration, initialization, element access, iteration, and various methods. By understanding these concepts, you can effectively use std::array in your C++ projects to write clean, efficient, and safe code.


PreviousIntroduction to STL & ContainersNext Vectors

Recommended Gear

Introduction to STL & ContainersVectors