Home Item 4 (1/2) - array of std (English)
Post
Cancel

Item 4 (1/2) - array of std (English)

c++ New Container Type: array

Think C++’s array is too cumbersome? Like int[5], it doesn’t offer many member functions to assist coders. No worries, you can use std::array.

When you need a fixed-size array and know its size in advance, you can use C++11 std::array. It’s safer and more convenient than built-in arrays because it provides size information and some convenient member functions.

Let’s dive straight into the code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
#include <array>

int main()
{
    // Create a fixed-size array containing 5 integers
    std::array<int, 5> arr = { 1, 2, 3, 4, 5 };

    // Iterate over the array using a range-based for loop and print each element
    for (int x : arr) {
        std::cout << x << " ";
    }
    std::cout << std::endl;

    // size()
    std::cout << "array size:" << arr.size() << std::endl;

    // at()
    std::cout << "element at index 4:" << arr.at(4) << std::endl;

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

    // Reverse traversal using iterators
    std::cout << "Reverse traversal:" << std::endl;
    for (auto it = arr.rbegin(); it != arr.rend(); ++it) {
        std::cout << *it << " ";
    }
    std::cout << std::endl;

    // fill()
    arr.fill(0);

    // Print the result after fill(0)
    std::cout << "after fill(0)" << std::endl;
    for (auto it = arr.begin(); it != arr.end(); ++it) {
        std::cout << *it << " ";
    }
    std::cout << std::endl;

    return 0;
}

onlinegdb

Execution Result

Desktop View

Usage:

std::array provides some features that built-in arrays lack, including:

  1. Size Information: std::array contains size information about the array, allowing you to know the size of the array at compile time. This avoids using operators like sizeof to query the size of the array.
  2. Safe Index Access: std::array provides the at() function, which allows safe index access. It checks the validity of the index and throws an exception if the index is out of bounds.
  3. Iterator Support: std::array supports forward and reverse iterators, which can be obtained using the begin() and end() functions.
  4. Element Access: Member functions like front(), back(), and operator[] allow easy access to the first and last elements of the array, as well as access to specific elements of the array by index.
  5. Assignment and Fill: The fill() function sets all elements of the array to a specific value, and std::array supports copy assignment and move assignment.

The above code examples demonstrate it. Give std::array a try next time, and you’ll find the world much brighter. Ha!

☝ツ☝

This post is licensed under CC BY 4.0 by the author.

👈 ツ 👍