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 size()
member function of the std::array
container to obtain the size of the myArray
array, which is the number of elements it contains. 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 << "size of array is: " << myArray.size() << endl;
return 0;
}
size of array is: 9