This is a C++ program that demonstrates the use of the std::array
container.
The std::array
container is a fixed-size array that is part of the C++ Standard Library. It provides similar functionality to a built-in array, but with additional features such as bounds checking, size tracking, and iterator support.
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 std::array
container provides several member functions that can be used to access its elements. Two of these functions, front()
and back()
, are used in this program to print the first and last elements of the array, respectively.
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.front() << endl;
cout << myArray.back() << endl;
return 0;
}
This is because front()
returns the first element of the array (in this case, 1) and back()
returns the last element of the array (in this case, 9).
first element in array: 1
last element in array: 9