std::array
in C++
In C++11, a new array type called std::array
was introduced to provide a safer, more modern alternative to traditional C-style arrays. Unlike regular arrays, std::array
comes with various built-in methods for easier manipulation and access to elements, along with benefits like size checking and better integration with standard algorithms.
std::array
is part of the C++ Standard Library and is defined in the <array> header. It is a container that encapsulates fixed-size arrays and provides a more convenient, flexible way to handle arrays in modern C++.
std::array
std::array
is fixed at compile-time and cannot be changed during runtime.std::array
provides type safety, ensuring the elements are of the specified type.size()
, fill()
, swap()
, etc.).std::array
can perform bounds checking when accessing elements using member functions like at()
.
std::array<T, N> array_name;
T
: The data type of the elements.N
: The number of elements in the array, which is known at compile time.std::array
You can declare a std::array
like any other C++ container, but its size is fixed during compile-time.
#include <iostream>
#include <array>
int main() {
// Declare and initialize std::array
std::array<int, 5> arr = {1, 2, 3, 4, 5};
// Access and display elements
for (int i = 0; i < arr.size(); ++i) {
std::cout << arr[i] << " ";
}
return 0;
}
Explanation:
arr
is an array of 5 integers.size()
member function returns the number of elements in the array.[]
operator.at()
for Bounds CheckingUnlike traditional arrays, std::array
has an at()
member function that performs bounds checking.
#include <iostream>
#include <array>
int main() {
std::array<int, 3> arr = {10, 20, 30};
// Accessing elements using 'at()' with bounds checking
for (int i = 0; i < arr.size(); ++i) {
std::cout << arr.at(i) << " ";
}
// This will throw an exception if out-of-bounds access is attempted
// std::cout << arr.at(5) << std::endl; // Uncommenting this line will throw an exception
return 0;
}
Explanation:
at()
provides bounds checking, and it will throw an std::out_of_range
exception if an invalid index is accessed.std::array
std::array
provides a variety of functions that can make working with arrays easier.
size()
Returns the number of elements in the array.
std::array<int, 5> arr = {1, 2, 3, 4, 5};
std::cout << "Size: " << arr.size() << std::endl; // Output: 5
fill()
Fills the entire array with a specified value.
std::array<int, 3> arr = {1, 2, 3};
arr.fill(0); // Fills the array with 0
for (int i : arr) {
std::cout << i << " "; // Output: 0 0 0
}
swap()
Swaps the contents of two arrays of the same type and size.
std::array<int, 3> arr1 = {1, 2, 3};
std::array<int, 3> arr2 = {4, 5, 6};
arr1.swap(arr2); // Swaps arr1 and arr2
for (int i : arr1) {
std::cout << i << " "; // Output: 4 5 6
}
front()
and back()
Accesses the first and last element of the array.
std::array<int, 3> arr = {1, 2, 3};
std::cout << "First element: " << arr.front() << std::endl; // Output: 1
std::cout << "Last element: " << arr.back() << std::endl; // Output: 3
data()
Returns a pointer to the first element of the array.
std::array<int, 3> arr = {10, 20, 30};
int* ptr = arr.data();
std::cout << "First element via pointer: " << *ptr << std::endl; // Output: 10
std::array
Over C-style Arraysstd::array
ensures type safety.std::array
provides functions like size()
, at()
, fill()
, etc., that are not available in C-style arrays.std::array
works seamlessly with standard algorithms, such as std::sort
, std::find
, etc., unlike C-style arrays.at()
function offers safe access to elements with bounds checking, preventing out-of-bounds errors.std::array
Use std::array
when:
at()
).