The new arrays in C++ using the STL have many advantages over the old-style C-arrays. In this tutorial I explain for C++ beginners how to use the arrays.

Basically what you have to do in your C++ application is to include the array header. Then you can create you C++ STL array like this:

std::array<int, 4> myarray = { 4, 7, 8, 100 );

As you can see it is templae based, this means you can define your datatype in angle brackets.

When you use C++ 17 it is also possible to let the compiler deduce the type and you can define your array like that:

std::array myarray = { 4, 7, 8, 100 );

For these arrays you can also use the range-based for loop to iterate the entries:

std::sort(myArray.begin(), myArray.end(),
std::greater_equal());

for (int x : myArray)
{
    std::cout &lt;&lt; x &lt;&lt; std::endl;
}

(Visited 52 times, 1 visits today)