Arrays in STL are sequential containers that are very similar to static arrays. They are continuous blocks of memory and have static sizes. Although there are very similar to static arrays in C++, their member functions make them more efficient. They are implemented as static arrays in memory. The array is present in #include<array> header file.
Array Declaration
Syntax:
array<data_type,size> name;
data_type: The data types of the elements to be stored in the array
size: The size of the array
Example:
array < int, 5 > arr; //initializes a array of size 5 which stores integer values Array Initialization
Syntax:
array<data_type,size> name{initial_values};
initial_values: optional parameter which initializes the array with the given values.
Example:
array < int, 5 > arr { 1, 2, 3, 4, 5 }; //initializes a array of size 5 having intial values 1,2,3,4,5Functions on arrays
[i]: It is used to access the element stored at index i.
Time Complexity: O(1)
Parameters: index of the element
Return type: element at index i
at(i): It is also used to access the element stored at index i.
Time Complexity: O(1)
Parameters: index of the element
Return type: element at index i if i in range of array otherwise out_of_bound error is thrown
front(): It returns the first element of the array.
Parameters: None
Return type: the first element of the array
back(): It returns the last element of the array.
Parameters: None
Return type: last element of the array.
size(): It tells us the number of elements in the array.
Parameters: None
Return type: integer - elements in the vector
fill(element): It inserts the given value at every array index.
Parameters: value to be inserted.
Return type: void
front(): It returns an iterator pointing to the first element of the array.
Parameters: None
Return type: iterator
back(): It returns an iterator pointing to the last element of the array.
Parameters: None
Return type: iterator
empty(): It tells us whether the array is empty or not.
Parameters: None
Return type: Boolean, true if the array is empty else false
data(): It returns a pointer pointing to the first element of the array.
Parameters: None
Return type: iterator, pointing to the first element of the array
swap(array_name): It swaps the content of the two arrays.
Parameters: array_name to be swapped with
Return type: void
#include<iostream>
#include<array>
using namespace std;
int main() {
array < int, 5 > arr { 1, 2, 3, 4, 5 };
for (int i = 0; i < 5; i++) {
cout << arr[i] << " ";
}
cout << '\n';
cout << "Size of array is " << arr.size() << '\n';
cout << "First element of array is " << arr.front() << '\n';
cout << "Last element of array is " << arr.back() << '\n';
cout << "Second element of array is " << arr.at(1) << '\n';
cout << "First element of array using data method is " << * (arr.data()) << '\n';
arr.fill(10);
cout << "The new array is ";
for (int i = 0; i < 5; i++) {
cout << arr[i] << " ";
}
cout << '\n';
if (arr.empty() == false) {
cout << "array is not empty!";
}
return 0;
}Output
1 2 3 4 5
Size of array is 5
First element of array is 1
Last element of array is 5
Second element of array is 2
First element of array using data method is 1
The new array is 10 10 10 10 10
array is not empty!