std::array
std::array
is a container in the C++ Standard Library that represents a fixed-size array. Unlike traditional C-style arrays, std::array provides a more convenient and safer interface with additional functionalities. std::array
is part of the sequence containers, along with std::vector
, std::deque
, and others, but it differs in that its size is fixed at compile time and cannot be changed.
std::array
std::array
is determined at compile time and cannot be changed after the array is created.std::array
are stored contiguously in memory, just like C-style arrays, allowing for efficient access.Safety:
std::array provides type safety and bounds checking (in debug mode) when accessing elements, reducing the risk of common errors like buffer overflows.std::array
provides a standard container interface, including methods like begin()
, end()
, size()
, and empty()
, making it more versatile than C-style arrays.std::vector
, std::array
does not perform dynamic memory allocation, which can be beneficial in performance-critical code where predictable memory usage is important.std::array
Let's explore some examples to understand how std::array
works.
#include <iostream>
#include <array>
int main() {
// Create a std::array of 5 integers
std::array<int, 5> myArray = {1, 2, 3, 4, 5};
// Access elements using the index operator
std::cout << "First element: " << myArray[0] << std::endl;
std::cout << "Second element: " << myArray[1] << std::endl;
// Iterate over the array using a range-based for loop
std::cout << "Elements: ";
for (int value : myArray) {
std::cout << value << " ";
}
std::cout << std::endl;
return 0;
}
std::array<int, 5>
declares a fixed-size array of 5 integers.{1, 2, 3, 4, 5}
.[]
, just like with C-style arrays.std::array
.std::array
integrates seamlessly with standard algorithms provided by the C++ Standard Library.
#include <iostream>
#include <array>
#include <algorithm>
int main() {
std::array<int, 5> myArray = {5, 3, 4, 1, 2};
// Sort the array
std::sort(myArray.begin(), myArray.end());
// Find an element
auto it = std::find(myArray.begin(), myArray.end(), 3);
// Print sorted array
std::cout << "Sorted elements: ";
for (int value : myArray) {
std::cout << value << " ";
}
std::cout << std::endl;
// Print the result of the find operation
if (it != myArray.end()) {
std::cout << "Found element: " << *it << std::endl;
} else {
std::cout << "Element not found" << std::endl;
}
return 0;
}
std::sort
is used to sort the elements of the array. std::array
provides iterators through begin()
and end()
that work with standard algorithms.std::find
is used to search for an element in the array.std::array
can be used with standard algorithms like sort
and find
.std::array
provides several useful member functions to access its properties, such as size()
, empty()
, front()
, back()
, and data()
.
#include <iostream>
#include <array>
int main() {
std::array<int, 5> myArray = {10, 20, 30, 40, 50};
std::cout << "Array size: " << myArray.size() << std::endl;
std::cout << "First element: " << myArray.front() << std::endl;
std::cout << "Last element: " << myArray.back() << std::endl;
int* dataPtr = myArray.data();
std::cout << "Data pointer: " << dataPtr << ", First element via pointer: " << *dataPtr << std::endl;
return 0;
}
size()
returns the number of elements in the array.front()
and back()
return the first and last elements of the array, respectively.data()
returns a pointer to the underlying array, allowing interoperability with C-style APIs that require raw pointers.std::array
can be used to create multidimensional arrays by nesting std::array types
.
#include <iostream>
#include <array>
int main() {
// Create a 2D array (3x3 matrix)
std::array<std::array<int, 3>, 3> matrix = {{
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
}};
// Access and print the elements of the 2D array
for (const auto& row : matrix) {
for (int value : row) {
std::cout << value << " ";
}
std::cout << std::endl;
}
return 0;
}
3x3
array, where each element is itself an std::array<int, 3>
.The at() method in std::array provides bounds-checked access to elements, throwing an exception if the index is out of bounds.
#include <iostream>
#include <array>
int main() {
std::array<int, 5> myArray = {10, 20, 30, 40, 50};
try {
std::cout << "Element at index 2: " << myArray.at(2) << std::endl;
std::cout << "Element at index 5: " << myArray.at(5) << std::endl; // This will throw an exception
} catch (const std::out_of_range& e) {
std::cerr << "Exception: " << e.what() << std::endl;
}
return 0;
}
at()
checks whether the index is within bounds and throws an std::out_of_range
exception if it is not.try-catch
.size()
.std::array
: A fixed-size array container in the C++ Standard Library that provides a safe and convenient alternative to C-style arrays.std::array
is determined at compile time and cannot be changed.begin()
, end()
, size()
, and at()
for safe and efficient array manipulation.std::array
is an essential tool in C++ programming when you need a fixed-size array with the benefits of type safety, a standard interface, and better integration with modern C++ features. It provides the efficiency of C-style arrays while avoiding many of the pitfalls associated with raw arrays.