Just for future reference. Since C++11 std::array was introduced in Standard Library. It should be preferred than C-like built-in array.
Example:
#include <array>
std::array<int, 10000000> myArray;
// fill the array with data
std::sort(myArray.begin(); myArray.end()); // sorts data using default operator <
More information about this container can be found on Bjarne homepage: http://www.stroustrup.com/C++11FAQ.html#std-array
The standard container array is a fixed-sized random-access sequence of elements defined in . It has no space overheads beyond what it needs to hold its elements, it does not use free store, it can be initialized with an initializer list, it knows its size (number of elements), and doesn't convert to a pointer unless you explicitly ask it to. In other words, it is very much like a built-in array without the problems.
Additional example:
#include <array> // std::array
#include <algorithm> // std::sort
#include <iostream> // std::cout
int main()
{
std::array<int, 10> myArray = {8, 3, 6, 7}; // array with 10 elements but create it with declaring only first 4 elements
std::cout << "BEFORE SORT:" << std::endl;
for (const auto& element : myArray)
{
std::cout << element << std::endl;
}
std::sort(myArray.begin(), myArray.end()); // sorts data using default operator <
std::cout << "AFTER SORT:" << std::endl;
for (const auto& element : myArray)
{
std::cout << element << std::endl;
}
}
Output:
BEFORE SORT:
8
3
6
7
0
0
0
0
0
0
AFTER SORT:
0
0
0
0
0
0
3
6
7
8