The std::array class, introduced in C++11, is a fixed-size container that provides a safer and more convenient alternative to traditional C-style arrays. It combines the performance of static arrays with the functionality of STL containers.
- Stores elements in contiguous memory while providing STL container features.
- Maintains size information and supports iterators, algorithms, and utility functions.
#include <array>
#include <algorithm>
#include <iostream>
using namespace std;
int main()
{
array<int, 5> arr = {5, 4, 3, 2, 1};
sort(arr.begin(), arr.end());
for (auto x : arr)
cout << x << " ";
return 0;
}
Output
1 2 3 4 5
Explanation: The example creates a fixed-size array, sorts it using the STL sort() algorithm, and prints the elements using a range-based loop.
Syntax
array<data_type, size> array_name;
Where:
- data_type specifies the type of elements.
- size specifies the fixed size of the array.
- array_name is the name of the array object.
To use std::array, include the following header file:
#include <array>
Common Member Functions of std::array
The std::array class provides several member functions for accessing and manipulating elements efficiently.
1. at(), get(), and operator[]
at(), get(), and operator[] functions are used to access elements of the array.
#include <array>
#include <tuple>
#include <iostream>
using namespace std;
int main()
{
array<int, 5> arr = {1, 2, 3, 4, 5};
cout << arr.at(2) << endl;
cout << get<3>(arr) << endl;
cout << arr[4] << endl;
return 0;
}
Output
3 4 5
Explanation: at() performs bounds checking, get() accesses elements using compile-time indices, and operator[] behaves similarly to C-style arrays.
2. front() and back()
The front() function return reference to the first and back() function return reference to the last elements of the array.
#include <array>
#include <iostream>
using namespace std;
int main()
{
array<int, 5> arr = {1, 2, 3, 4, 5};
cout << arr.front() << endl;
cout << arr.back() << endl;
return 0;
}
Output
1 5
3. size() and max_size()
The size() function returns the number of elements in an array and max_size() returns the maximum number of elements array can holdĀ
#include<iostream>
#include<array>
using namespace std;
int main()
{
array<int,6> ar = {1, 2, 3, 4, 5, 6};
cout << ar.size() << endl;
cout << ar.max_size() << endl;
return 0;
}
Output
6 6
4. swap()
The swap() function exchanges the contents of two arrays.
#include <array>
#include <iostream>
using namespace std;
int main()
{
array<int, 3> a = {1, 2, 3};
array<int, 3> b = {4, 5, 6};
a.swap(b);
for (auto x : a)
cout << x << " ";
return 0;
}
Output
4 5 6
5. empty() and fill()
empty() and fill() functions check whether the array is empty and assign values to all elements.
#include <array>
#include <iostream>
using namespace std;
int main()
{
array<int, 5> arr;
arr.fill(10);
cout << boolalpha << arr.empty() << endl;
for (auto x : arr)
cout << x << " ";
return 0;
}
Output
false 10 10 10 10 10
6. data()
The data() function returns a pointer to the first element of the array.
#include <array>
#include <iostream>
using namespace std;
int main()
{
array<int, 3> arr = {1, 2, 3};
int* ptr = arr.data();
cout << *ptr;
return 0;
}
Output
1
Advantages of std::array over C-style Arrays
The std::array class provides several advantages over traditional arrays:
- Stores its size internally, eliminating the need to pass the size separately.
- Does not decay into pointers when passed to functions.
- Supports STL algorithms and iterator-based operations.
- Provides safer element access through member functions.
- Offers better reliability and compatibility with other STL components.
Limitations of std::array
Despite its advantages, std::array has some limitations:
- The size of a std::array must be known at compile time.
- It cannot grow or shrink after the array has been created.
- It is less flexible than dynamic containers such as std::vector.