O'Reilly logo

Mastering C++ Programming by Jeganathan Swaminathan

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Array

The STL array container is a fixed-size sequence container, just like a C/C++ built-in array, except that the STL array is size-aware and a bit smarter than the built-in C/C++ array. Let's understand an STL array with an example:

#include <iostream>#include <array>using namespace std;int main () {  array<int,5> a = { 1, 5, 2, 4, 3 };  cout << "\nSize of array is " << a.size() << endl;  auto pos = a.begin();  cout << endl;  while ( pos != a.end() )     cout << *pos++ << "\t";  cout << endl;  return 0;}

The preceding code can be compiled and the output can be viewed with the following commands:

g++ main.cpp -std=c++17./a.out

The output of the program is as follows:

Size of array is 51     5     2     4     3

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required