at() – get element by index

This is a C++ program that demonstrates the use of the std::array container to access an element by index.

In this program, an std::array object called myArray is declared and initialized with the values 1 through 9. The std::array container is defined by specifying the element type and the size of the array in angle brackets. In this case, the element type is int and the size is determined by the number of initializers provided.

The program then uses the index operator [] to access the element at index 4 of the myArray array, which corresponds to the value 5. The value is then printed to the console using the cout statement.

The using namespace std; statement at the beginning of the program allows the program to use names from the std namespace without explicitly qualifying them.

#include <iostream>
#include <array>
using namespace std;

int main() {
    array<int, 9> myArray = {1, 2, 3, 4, 5, 6, 7, 8, 9};

    cout << myArray[4] << endl;

    return 0;
}
Output
5