Tutorial by Examples

Initializing std::array<T, N>, where T is a scalar type and N is the number of elements of type T If T is a scalar type, std::array can be initialized in the following ways: // 1) Using aggregate-initialization std::array<int, 3> a{ 0, 1, 2 }; // or equivalently std::array<int, 3...
1. at(pos) Returns a reference to the element at position pos with bounds checking. If pos is not within the range of the container, an exception of type std::out_of_range is thrown. The complexity is constant O(1). #include <array> int main() { std::array<int, 3> arr; ...
One of the main advantage of std::array as compared to C style array is that we can check the size of the array using size() member function int main() { std::array<int, 3> arr = { 1, 2, 3 }; cout << arr.size() << endl; }
std::array being a STL container, can use range-based for loop similar to other containers like vector int main() { std::array<int, 3> arr = { 1, 2, 3 }; for (auto i : arr) cout << i << '\n'; }
The member function fill() can be used on std::array for changing the values at once post initialization int main() { std::array<int, 3> arr = { 1, 2, 3 }; // change all elements of the array to 100 arr.fill(100); }

Page 1 of 1