What is an Array Container?
At its core, an array container in C++'s STL represents a fixed-size sequence of elements stored in contiguous memory locations. Unlike other STL containers such as vectors or lists, which offer dynamic resizing and flexible memory location, array containers have a fixed size determined at compile time. This fixed-size nature makes array containers suitable for scenarios where the size of the collection is known in advance and does not change during program execution.
Characteristics of Array Container
Contiguous Memory Allocation:
- Array containers store elements in contiguous memory locations, resulting in efficient memory access patterns and cache utilization.
- This contiguous memory allocation allows for efficient traversal and manipulation of elements using pointer arithmetic.
Fixed-Size Nature:
- Array containers have a fixed size determined at compile time, specified by the array's type and size template parameters.
- Once created, the size of the array container cannot be changed dynamically during program execution, making it suitable for scenarios where a fixed-size collection is sufficient.
Random Access:
- Array containers support random access to elements using array indexing.
- Elements in the array can be accessed directly by their index, allowing for efficient element retrieval and modification.
Efficient Element Access:
- Accessing elements in an array container has a constant-time complexity O(1) because elements are stored in contiguous memory locations.
- This efficient element access makes array containers suitable for scenarios where fast element retrieval is essential.
Syntax Of Array Container
#include <array> // Include the array header
int main() {
// Declaration and Initialization
std::array<DataType, Size> myArray; // Create an array of fixed size
std::array<DataType, Size> myArray = {val1, val2, val3}; // Create and initialize with values
// Element Access
myArray[index]; // Access element at index 'index'
// Size
myArray.size(); // Returns the size of the array
return 0;
}
Example:
#include <iostream>
#include <array>
int main() {
// Creating an array of integers
std::array<int, 5> myArray = {1, 2, 3, 4, 5};
// Accessing elements and displaying them
for (int i = 0; i < myArray.size(); ++i) {
std::cout << myArray[i] << " ";
}
std::cout << std::endl;
return 0;
}
Functions
1 at()
This function is used to access the element at a specified position in the array. It performs bounds-checking to ensure that the specified index is within the valid range of the array.
#include <iostream>
#include <array>
int main() {
std::array<int, 5> myArray = {1, 2, 3, 4, 5};
// Accessing element at index 2
std::cout << "Element at index 2: " << myArray.at(2) << std::endl;
return 0;
}
Complexity:
- The time complexity of
at()
is constantO(1)
because it directly accesses the element at the specified index without any traversal.
2 operator[]
This is used to access the element at a specified position in the array. It does not perform bounds-checking, so it's more efficient but less safe than at()
.
#include <iostream>
#include <array>
int main() {
std::array<int, 5> myArray = {1, 2, 3, 4, 5};
// Accessing element at index 2
std::cout << "Element at index 2: " << myArray[2] << std::endl;
return 0;
}
Complexity:
- The time complexity of
operator[]
is constantO(1)
because it directly accessed the element at the specified index without any traversal.
3 size()
This function returns the number of elements in the array.
#include <iostream>
#include <array>
int main() {
std::array<int, 5> myArray = {1, 2, 3, 4, 5};
// Getting the size of the array
std::cout << "Size of the array: " << myArray.size() << std::endl;
return 0;
}
Complexity:
- The time complexity of
size()
is constantO(1)
because it returns the size of the array directly, without any traversal.
4 front()
This function returns a reference to the first element in the array.
#include <iostream>
#include <array>
int main() {
std::array<int, 5> myArray = {1, 2, 3, 4, 5};
// Accessing the first element of the array
std::cout << "First element: " << myArray.front() << std::endl;
return 0;
}
Complexity:
- The time complexity of
front()
is constantO(1)
because it directly returns a reference to the first element.
5 back()
This function returns a reference to the last element in the array.
#include <iostream>
#include <array>
int main() {
std::array<int, 5> myArray = {1, 2, 3, 4, 5};
// Accessing the last element of the array
std::cout << "Last element: " << myArray.back() << std::endl;
return 0;
}
Complexity:
- The time complexity of
back()
is constantO(1)
because it directly returns a reference to the last element.