Cpp Libs Array
C++11 standard introduced the `` header file, which provides a fixed-size array container. Compared with arrays in C language, it has better type safety and memory management features.
`std::array` is a template class in the C++ standard library, defined in the `` header file. The `std::array` template class provides a fixed-size array whose size is determined at compile time and cannot be dynamically changed.
### Syntax
The basic syntax of `std::array` is as follows:
#include std::array array_name;
* `T` is the type of elements in the array.
* `N` is the size of the array, which must be a non-negative integer.
### Declaration and Initialization
`` requires the size to be determined at compile time and cannot be dynamically changed. Usage example:
#include #include int main() { std::array arr = {1, 2, 3, 4, 5}; // Declare a fixed-length array of 5 integers return 0;}
## Features
* **Type Safety**: `std::array` enforces type checking, avoiding the type safety issues of C-style arrays.
* **Fixed Size**: The array size is determined at compile time and cannot be changed at runtime.
* **Contiguous Memory**: The elements of `std::array` are stored contiguously in memory, allowing efficient element access.
* **Standard Container**: `std::array` provides interfaces similar to `std::vector`, such as `size()`, `at()`, `front()`, `back()`, etc.
## Example
Below is a simple example of using `std::array`, including the output result.
## Example
#include
#include
int main(){
// Create a std::array containing 5 integers
std::array myArray ={1, 2, 3, 4, 5};
// Use range-based for loop to iterate through the array
for(const auto& value : myArray){
std::cout<< value <<" ";
}
std::cout<< std::endl;
// Access array elements using index
std::cout<<"Element at index 2: "<< myArray.at(2)<< std::endl;
// Get the size of the array
std::cout<<"Array size: "<< myArray.size()<< std::endl;
// Modify array elements
myArray=10;
// Iterate through the array again to show modified elements
for(const auto& value : myArray){
std::cout<< value <<" ";
}
std::cout<< std::endl;
return 0;
}
Output:
1 2 3 4 5 Element at index 2: 3Array size: 51 2 3 10 5
* * *
## Common Member Functions
Here are some commonly used member functions in ``:
| Function | Description |
| --- | --- |
| `at(size_t pos)` | Returns the element at the specified position with bounds checking |
| `operator[]` | Returns the element at the specified position without bounds checking |
| `front()` | Returns the first element of the array |
| `back()` | Returns the last element of the array |
| `data()` | Returns a pointer to the array data |
| `size()` | Returns the array size (fixed) |
| `fill(const T& value)` | Sets all array elements to the specified value |
| `swap(array& other)` | Swaps the contents of two arrays |
| [`begin()` / `end()`](#) | Returns the start/end iterators of the array |
### Example
**1. Basic Operations**
## Example
#include
#include
int main(){
std::array arr ={10, 20, 30, 40, 50};
std::cout<<"Array elements: ";
for(int i =0; i < arr.size();++i){
std::cout<< arr<<" ";
}
std::cout<< std::endl;
// Get the first and last elements
std::cout<<"First element: "<< arr.front()<< std::endl;
std::cout<<"Last element: "<< arr.back()<< std::endl;
return 0;
}
**Using at with Bounds Checking**
## Example
#include
#include
int main(){
std::array arr ={1, 2, 3};
try{
std::cout<< arr.at(2)<< std::endl;// Normal output
std::cout<< arr.at(5)<< std::endl;// Out of range, throws exception
}catch(const std::out_of_range& e){
std::cout<<"Exception: "<< e.what()<< std::endl;
}
return 0;
}
**3. Using fill to Populate Elements**
## Example
#include
#include
int main(){
std::array arr;
arr.fill(100);// Set all elements to 100
std::cout<<"Filled array: ";
for(const auto& elem : arr){
std::cout<< elem <<" ";
}
std::cout<< std::endl;
return 0;
}
**4. Array Swap**
## Example
#include
#include
int main(){
std::array arr1 ={1, 2, 3};
std::array arr2 ={4, 5, 6};
arr1.swap(arr2);
std::cout<<"Array 1: ";
for
YouTip