STD::array in C++
The array is a container for constant size arrays. This container wraps around fixed size arrays and also doesn’t loose the information of its length when decayed to a pointer.
In order to utilize array, we need to include the array header:
#include <array>
Let’s see an example.
// CPP program to demonstrate working of array #include <algorithm> #include <array> #include <iostream> #include <iterator> #include <string> using namespace std; int main() { // construction uses aggregate initialization // double-braces required array<int, 5> ar1{{3, 4, 5, 1, 2}}; array<int, 5> ar2 = {1, 2, 3, 4, 5}; array<string, 2> ar3 = {{string("a"), "b"}}; cout << "Sizes of arrays are" << endl; cout << ar1.size() << endl; cout << ar2.size() << endl; cout << ar3.size() << endl; cout << "\nInitial ar1 : "; for (auto i : ar1) cout << i << ' '; // container operations are supported sort(ar1.begin(), ar1.end()); cout << "\nsorted ar1 : "; for (auto i : ar1) cout << i << ' '; // Filling ar2 with 10 ar2.fill(10); cout << "\nFilled ar2 : "; for (auto i : ar2) cout << i << ' '; // ranged for loop is supported cout << "\nar3 : "; for (auto &s; : ar3) cout << s << ' '; return 0; } |
chevron_right
filter_none
Output:
Sizes of arrays are 5 5 2 Initial ar1 : 3 4 5 1 2 sorted ar1 : 1 2 3 4 5 Filled ar2 : 10 10 10 10 10 ar3 : a b
Recommended Posts:
- Minimum cells to be flipped to get a 2*2 submatrix with equal elements
- Nested Loops in C++ with Examples
- _Find_first() function in C++ bitset with Examples
- _Find_next() function in C++ bitset with Examples
- Left-Right traversal of all the levels of N-ary tree
- Difference between Iterators and Pointers in C/C++ with Examples
- ostream::seekp(pos) method in C++ with Exmaples
- Default Methods in C++ with Examples
- C++ Tutorial
- Hello World Program : First program while learning Programming
- Difference between Argument and Parameter in C/C++ with Examples
- <cfloat> float.h in C/C++ with Examples
- C/C++ #include directive with Examples
- C/C++ if else statement with Examples
- C/C++ if statement with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

