std::array is a fixed-size container provided by the C++ Standard Template Library (STL). It acts as a wrapper around built-in arrays while providing additional functionality such as iterators, size information, and various utility member functions.
- Stores elements in contiguous memory like a normal array.
- Provides STL container features such as iterators and utility functions.
- The size of a std::array is fixed at compile time.
#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;
}
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
Explanation: The std::array container supports STL algorithms and utility functions. Unlike built-in arrays, it retains size information and provides member functions such as size(), fill(), and iterator support.
syntax
std::array<data_type, size> array_name;
To use std::array, include the following header:
#include <array>
Common Member Functions of std::array
std::array provides several built-in member functions that make it more convenient and safer to use than traditional C-style arrays.
- Provides functions for accessing, modifying, and querying array elements.
- Supports STL-style iterators and utility operations for efficient programming.
[] Operator
Accesses the element stored at the specified index. It does not perform bounds checking, so accessing an invalid index results in undefined behavior.
#include <iostream>
#include <array>
using namespace std;
int main() {
array <char , 3> arr={'G','f','G'};
cout<<arr[0] <<" "<<arr[2];
return 0;
}
Output
G G
front() and back()
The front() function returns a reference to the first element of the array. It provides direct access to the element stored at the beginning of the container and back() functions return the first and last elements of the array.
#include <iostream>
#include <array>
using namespace std;
int main() {
array <int , 3> arr={'G','f','G'}; // ASCII val of 'G' =71
cout<<arr.front() <<" "<<arr.back();
return 0;
}
Output
71 71
swap()
The swap() function returns a reference to the last element of the array. It is useful when the last stored element needs to be accessed directly.
#include <iostream>
#include <array>
using namespace std;
int main() {
array <int , 3> arr={'G','f','G'}; // ASCII val of 'G' =71
array <int , 3> arr1={'M','M','P'}; // ASCII val of 'M' = 77 and 'P' = 80
arr.swap(arr1); // now arr = {M,M,P}
cout<<arr.front() <<" "<<arr.back();
return 0;
}
Output
77 80
empty()
The empty() function checks whether the array contains any elements and returns a boolean value. Since std::array has a fixed size, it is usually false unless the array size is zero.
#include <iostream>
#include <array>
using namespace std;
int main() {
array <int , 3> arr={'G','f','G'}; // ASCII val of 'G' =71
array <int , 3> arr1={'M','M','P'}; // ASCII val of 'M' = 77 and 'P' = 80
bool x = arr.empty(); // false ( not empty)
cout<<boolalpha<<(x);
return 0;
}
Output
false
at( ) function
The at( ) function accesses the element at the specified position while performing bounds checking. It throws a std::out_of_range exception if the index is invalid.
#include <iostream>
#include <array>
using namespace std;
int main() {
array <int , 3> arr={'G','f','G'}; // ASCII val of 'G' =71
array <int , 3> arr1={'M','M','P'}; // ASCII val of 'M' = 77 and 'P' = 80
cout<< arr.at(2) <<" " << arr1.at(2);
//cout<< arr.at(3); // exception{Abort signal from abort(3) (SIGABRT)}
return 0;
}
Output
71 80
fill()
The fill() function assigns the specified value to every element of the array. It provides a convenient way to initialize or reset all elements.
#include <iostream>
#include <array>
using namespace std;
int main() {
array <int , 5> arr;
arr.fill(1);
for(int i: arr)
cout<<arr[i]<<" ";
return 0;
}
Output
1 1 1 1 1
size() and max_size()
The size() function returns the total number of elements stored in the array. The returned value remains constant throughout the lifetime of the array and max_size() function return the number of elements in the array.
#include <iostream>
#include <array>
using namespace std;
int main() {
array <int , 10> arr;
cout<<arr.size()<<'\n'; // total num of indexes
cout<<arr.max_size()<<'\n'; // total num of indexes
cout<<sizeof(arr); // total size of array
return 0;
}
Output
10 10 40
data()
The data() function returns a pointer to the underlying contiguous memory storing the array elements. It can be used to access the array as a raw C-style array.
#include <iostream>
#include <cstring>
#include <array>
using namespace std;
int main ()
{
const char* str = "GeeksforGeeks";
array<char,13> arr;
memcpy (arr.data(),str,13);
cout << arr.data() << '\n';
return 0;
}
Output
GeeksforGeeks
begin(), end(), cbegin(), cend()
begin(), end() functions return iterators to the beginning and end of the array. cbegin(), cend() return constant iterators that provide read-only access to the elements and are commonly used with STL algorithms and range-based traversals.
#include <iostream>
#include <array>
#include <algorithm>
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
Advantages of std::array
std::array provides several advantages over built-in arrays by combining fixed-size storage with the features and safety of STL containers.
- Stores size information internally and works seamlessly with STL algorithms.
- Provides safer element access through at() and supports iterators and utility functions.
- Offers better compatibility and integration with other STL containers and components.
Limitations of std::array
Despite its advantages, std::array has some limitations due to its fixed-size nature.
- The size of the array must be specified at compile time and cannot be changed later.
- It does not support dynamic resizing, insertion, or deletion of elements.
- It is less flexible than dynamic containers such as std::vector.